Twig在单击隐藏时将变量更改为false



我有一个Twig变量,我用它来显示介绍部分,然后显示实际形式。

可变

{% set intro = true %}

如果为true,则会显示介绍部分,例如下面的部分。

{% if intro == true %}
<div class="Intro Wrapper Row">
<div class="form-container">
<div>
<div>
<button onclick="{{ intro|false }}">Lets Begin</button>
</div>
</div>
{% endif %}
{% if intro == false %}
<div class="SurveyFormLogin Wrapper Row">
<div class="form-container">
</div>
</div>

在Twig中,我如何将简介设置为false,以便隐藏简介并显示调查?或者这是在Twig中处理这一问题的最佳方式?

您的Twig模板在服务器端加载并呈现,也就是说,生成的HTML在服务器端生成,然后通过HTTP响应(可能(将HTML发送到浏览器,然后在那里显示。因此,你打算让你的Twig更具动态性,这意味着你需要给它传递一个值。例如:

$app["twig"]->addGlobal("intro", $isIntro);

其中CCD_ 1被假定为正确初始化的布尔值。

编辑

指定非全局Twig参数值的示例:

return $this->render('user/notifications.html.twig', [
// this array defines the variables passed to the template,
// where the key is the variable name and the value is the variable value
// (Twig recommends using snake_case variable names: 'foo_bar' instead of 'fooBar')
'user_first_name' => $userFirstName,
'notifications' => $userNotifications,
]);

最新更新