Twig

灵活、快速且安全的
PHP 模板引擎

a Symfony Product
Docs Tests defined
您正在阅读 Twig 3.x 的文档。切换到 Twig 1.x, 2.x 的文档。

问题 & 反馈

许可协议

Twig 文档 根据新的 BSD 许可协议获得许可。

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 %}