php - 如何在 Twig 中检查 null?

标签 php twig short-circuiting

我应该使用什么构造来检查 Twig 模板中的值是否为 NULL?

最佳答案

取决于你到底需要什么:

  • is null检查值是否为 null:

     {% if var is null %}
         {# do something #}
     {% endif %}
    
  • is defined检查变量是否已定义:

     {% if var is not defined %}
         {# do something #}
     {% endif %}
    

另外还有 is sameas test 对两个值进行类型严格比较,可能对检查 null 以外的值(如 false)感兴趣:

{% if var is sameas(false) %}
    {# do something %}
{% endif %}

关于php - 如何在 Twig 中检查 null?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3264889/

相关文章:

php - 如何重新排序搜索结果?

javascript - 如何使用 .htaccess 按文件名而不是扩展名缓存文件

java - 如何在 Java 中对返回 boolean 值的两个并行线程执行短路评估?

vb6 - 为什么短路不是 VB 中的默认行为?

python - Python的短路有什么值(value)?

php - php异常处理,无error_reporting

php - 在 Twig 中延迟执行/完成

php - Symfony 在 CheckboxType 中转换 ChoiceType

include - 交响乐 4 : Multiple inclusion of a file

javascript - 单击按钮时将输入字段值附加到 url