Twig的“不是无”语法

时间:2018-06-27 11:40:35

标签: symfony twig symfony-forms

在重载Symfony的表单模板时,我在choice_widget_collapsed中的form_div_layout.html.twig中遇到了奇怪的检查。

{%- block choice_widget_collapsed -%}
    {%- if required and placeholder is none and not placeholder_in_choices and not multiple and (attr.size is not defined or attr.size <= 1) -%}
        {% set required = false %}
    {%- endif -%}
    <select {{ block('widget_attributes') }}{% if multiple %} multiple="multiple"{% endif %}>
        {%- if placeholder is not none -%}
            <option value=""{% if required and value is empty %} selected="selected"{% endif %}>{{ placeholder != '' ? (translation_domain is same as(false) ? placeholder : placeholder|trans({}, translation_domain)) }}</option>
        {%- endif -%}
        {%- if preferred_choices|length > 0 -%}
            {% set options = preferred_choices %}
            {{- block('choice_widget_options') -}}
            {%- if choices|length > 0 and separator is not none -%}
                <option disabled="disabled">{{ separator }}</option>
            {%- endif -%}
        {%- endif -%}
        {%- set options = choices -%}
        {{- block('choice_widget_options') -}}
    </select>
{%- endblock choice_widget_collapsed -%}

if placeholder is not none是什么意思?我在Twig文档中没有遇到过这种语法,并且在谷歌搜索时发现只有从同一文件中复制的代码,没有任何解释。

我很好奇,为什么不is not nullis not emptyis defined呢? none在哪里定义?

2 个答案:

答案 0 :(得分:2)

Twig Null docs中所述 none是Twig语法中null的别名。

答案 1 :(得分:1)

测试none只是测试null的别名,如Twig的{​​{3}}所示:

public function getTests()
{
    return array(
        new Twig_Test('even', null, array('node_class' => 'Twig_Node_Expression_Test_Even')),
        new Twig_Test('odd', null, array('node_class' => 'Twig_Node_Expression_Test_Odd')),
        new Twig_Test('defined', null, array('node_class' => 'Twig_Node_Expression_Test_Defined')),
        new Twig_Test('same as', null, array('node_class' => 'Twig_Node_Expression_Test_Sameas')),
        new Twig_Test('none', null, array('node_class' => 'Twig_Node_Expression_Test_Null')),
        new Twig_Test('null', null, array('node_class' => 'Twig_Node_Expression_Test_Null')),
        new Twig_Test('divisible by', null, array('node_class' => 'Twig_Node_Expression_Test_Divisibleby')),
        new Twig_Test('constant', null, array('node_class' => 'Twig_Node_Expression_Test_Constant')),
        new Twig_Test('empty', 'twig_test_empty'),
        new Twig_Test('iterable', 'twig_test_iterable'),
    );
}