当重载Symfony的表单模板时,我在form_div_layout.html.twig
choice_widget_collapsed
中遇到了奇怪的检查。
{%- 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 文档中所述none
是 Twig 语法中null
的别名。
测试none
只是测试null
的别名,如Twig
的核心扩展所示:
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'),
);
}