defined
defined
检查变量是否在当前上下文中定义。如果您使用 strict_variables
选项,这将非常有用
1 2 3 4 5 6 7 8 9 10 11 12 13
{# defined works with variable names #}
{% if user is defined %}
...
{% endif %}
{# and attributes on variables names #}
{% if user.name is defined %}
...
{% endif %}
{% if user['name'] is defined %}
...
{% endif %}
当在表达式上使用 defined
测试时,该表达式在某些方法调用中使用了变量,请确保它们都首先被定义
1 2 3
{% if var is defined and user.name(var) is defined %}
...
{% endif %}