如何检查树枝中的变量是否为零并定义了值

How to check if a variable is equal to zero in twig and value defined

这似乎是一个非常基本的问题,但我向你保证,我已经 运行 了所有解决方案,但我仍然没有找到解决方案。

问题是这样的:

  1. 树枝值将由 select 框设置为 1、0 或 null,然后该值将用于设置 selected该框的值。

  2. 选择了两个过滤器 - 1 表示活动,0 表示不活动。

  3. 如果未设置任何值且 twig 值设置为空(null),则 0 的选项始终为 selected。

问题中的twig代码如下: <option value="null">Select an Option</option> <option value="1"{% if filterStatus == 1 %} selected{% endif %}>Active</option> <option value="0"{% if filterStatus == 0 %} selected{% endif %}>Inactive</option>

是我期望使用的。以下是我尝试过的众多变体之一:

{% if filterStatus == 0 and not filterStatus != 'null' %}

我似乎无法确保该值为 0。

也不要被选项值属性中的 "null" 值所迷惑。这用于路由,但在系统中转换为文字 NULL,而不是在它到达 twig 时转换为字符串。

非常感谢任何帮助。

非空校验方式为:

{% if var is not null %}

但是你可以使用same as函数:

{% if var is same as(0) %}
    {# do something %}
{% endif %}

参考:http://twig.sensiolabs.org/doc/tests/sameas.html

试试这个

{% if filterStatus == 0 and filterStatus is (not) empty %}