Twig 的逻辑运算符是否对两个表达式求值? [英] Do Twig's logical operators evaluate both expressions?

查看:28
本文介绍了Twig 的逻辑运算符是否对两个表达式求值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我使用 Twig 表达式,例如:

If I use a Twig expression like:

{% if a and function(a) %}

如果 a 为 false,Twig 是否仍然计算 function(a),或者表达式计算为 false 而不计算第二部分?与 类似.

with a being falsey, does Twig still evaluate function(a), or will the expression evaluate to false without evaluating the second part? Likewise with or.

推荐答案

tl;dr: Twig 的逻辑运算符不计算 'and' 表达式的第二部分,如果第一部分为假,同样使用 'or' 如果第一部分是真实的.

tl;dr: Twig's logical operators do not evaluate the second part of an 'and' expression if the first part is falsey, likewise with 'or' if the first part is truthy.

正如 zerkms 指出的,这是可测试的通过使用 die.

As pointed out by zerkms, this is testable by using die.

例如:

{% if water_is_dry and die('water_is_wet') %}

不会死,因为第一个表达式为空,是假的.

will not die, since the first expression, being null, is falsey.

鉴于:

{% if water_is_dry or die('water_is_wet') %}

会死.

请注意,这仅在您将 die 作为函数添加到 Twig 实例时才有效,如下所示:

Note, that this works only if you add die as a function to your Twig instance, like so:

$twig->addFunction(new Twig_SimpleFunction('die', 'die'));

这篇关于Twig 的逻辑运算符是否对两个表达式求值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆