Do Twig#39;s logical operators evaluate both expressions?(Twig 的逻辑运算符是否评估这两个表达式?)
问题描述
如果我使用 Twig 表达式,例如:
If I use a Twig expression like:
{% if a and function(a) %}
如果 a
为假,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"表达式的第二部分,同样,如果第一部分是真实的.
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 指出的,这是可测试的通过使用模具.
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 的逻辑运算符是否评估这两个表达式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Twig 的逻辑运算符是否评估这两个表达式?
- 从 PHP 中的输入表单获取日期 2022-01-01
- 正确分离 PHP 中的逻辑/样式 2021-01-01
- 带有通配符的 Laravel 验证器 2021-01-01
- Laravel 仓库 2022-01-01
- Oracle 即时客户端 DYLD_LIBRARY_PATH 错误 2022-01-01
- SoapClient 设置自定义 HTTP Header 2021-01-01
- Mod使用GET变量将子域重写为PHP 2021-01-01
- 如何定位 php.ini 文件 (xampp) 2022-01-01
- PHP Count 布尔数组中真值的数量 2021-01-01
- 没有作曲家的 PSR4 自动加载 2022-01-01