gpt4 book ai didi

twig - 如何正确使用 if not 语句与 Twig

转载 作者:行者123 更新时间:2023-12-04 01:53:29 25 4
gpt4 key购买 nike

我一直试图弄清楚如何在 Twig 模板引擎中使用 if not 子句。

根据 documents :

You can also use not to check for values that evaluate to false:



所以我想像这样使用它:
{% if not signed_in %}
You are not signed in.
{% endif %}
signed_in 来自中间件。看起来像这样:
    if (!$this->container->auth->check()) {
/* Render a Sign-up */
return $this->container->view->render($response, 'subscriber/index.twig', ['signed_in' => false]);
}

因此,如果我在未登录时使用 {{ dump(signed_in) }} ,它会返回 false 。但是当我登录时, singed_in 返回 null

Not signed in left. Signed in on right

我目前使用的解决方案是 {% if signed_in is defined %} ,但这实际上不是我要找的,或者是吗?

最佳答案

Twig != 是一个比较运算符。

The following comparison operators are supported in any expression: ==, !=, <, >, >=, and <=

您可以使用 != 来比较 php 允许您比较的任何两件事,并且您返回的是一个 bool 值。

Twig not 是一个逻辑运算符。 (也是和和或)
not: Negates a statement.

关于twig - 如何正确使用 if not 语句与 Twig ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38138727/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com