如果没有用树枝声明,如何正确使用

时间:2016-07-01 06:43:12

标签: twig

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

根据documents

  

您还可以使用 not 来检查评估为 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 %},但这实际上不是我想要的,或者是它?

2 个答案:

答案 0 :(得分:1)

Twig!=是比较运算符。

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

您可以使用!=比较php让您进行比较的任何两件事,而您得到的是布尔值。

Twig不是逻辑运算符。 (and和or也是如此)

not: Negates a statement.

答案 1 :(得分:0)

我建议你使用: {% if signed_in == true %}{% if signed_in == false%}

这是树枝比赛:

{% set testTrue = true %}
{% set testFalse = false %}
{% set testNull = null %}
{% set testString = "something" %}


{% if testTrue %} {# true #}
{% if testFalse %}{# false#}
{% if testNull %} {# false#}
{% if testString %} {# true #}

{% if not testTrue %}{# false#}
{% if not testFalse %}{# true#}
{% if not testNull %} {# false#}
{% if not testString %} {# false#}

{% if testTrue == true %} {# true #}
{% if testFalse == true  %} {# false#}
{% if testNull == true  %} {# false#}
{% if testNull == true  %} {# true #}

{% if not testTrue == true %} {# false#}
{% if not testFalse == true  %} {# true #}
{% if not testNull == true  %} {# true #}
{% if not testNull == true  %} {# false#}

其他测试要知道

{% if testTrue == 1 %} {# true #}
{% if testFalse == 1  %} {# false#}
{% if testTrue == "1" %} {# true #}
{% if testFalse == "1"  %} {# false#}
相关问题