Twig 的 `is not none` 语法
Twig's `is not none` syntax
当重载 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 null
、is not empty
、is defined
? none
定义在哪里?
如 Twig Null docs 中所述
none
是 null
在 Twig 语法中的别名。
测试 none
只是测试 null
的别名,如 Twig
的 core extension 所示:
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'),
);
}
当重载 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 null
、is not empty
、is defined
? none
定义在哪里?
如 Twig Null docs 中所述
none
是 null
在 Twig 语法中的别名。
测试 none
只是测试 null
的别名,如 Twig
的 core extension 所示:
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'),
);
}