- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在 PHP 中我可以做到
<?php if ($someVar): ?>
这会检查变量是否存在以及其值是否与类似零的值(如 0、null、'' 等)不同。
在 Twig 中有什么方法可以做到这一点,还是我需要为此编写自己的过滤器?此刻,我必须做
{% if someVar is defined and someVar %}
当涉及到更复杂的模板时,这是一个痛苦。
最佳答案
(至少)有两种方法可以在不扩展 Twig 的情况下做到这一点。第三种选择是通过创建例如来扩展 Twig。一个 Twig 函数。我可能会选择第一种方式(使用default
过滤器)。
默认
过滤器正如 @The_Unknown 指出的,您还可以使用 default
filter :
{% if someVar|default(null) %}
您可以省略向过滤器传递默认值,甚至省略括号。那么该值将默认为空字符串(这是错误的)。 IE。这两者是平等且有效的:
{% if someVar|default() %}
{% if someVar|default %}
无论您选择哪种样式(默认为 null
,省略值或省略括号),请坚持使用。保持一致。
See TwigFiddle演示真值的计算结果为 true
,假值的计算结果为 false
(基于下表)。
strict_variables
设置为 false
通过将环境变量 strict_variables
设置为 false
,您可以跳过 if someVar is Defined
部分,只执行 {% if someVar %}
。如 Twig's documentation 中所述:
strict_variables
booleanIf set to
false
, Twig will silently ignore invalid variables (variables and or attributes/methods that do not exist) and replace them with anull
value. When set totrue
, Twig throws an exception instead (default tofalse
).
创建 Twig_Environment
实例时将变量设置为 false
:
$twig = new Twig_Environment($loader, ['strict_variables' => false]);
如果someVar
未定义,那么{% if someVar %}
显然是false
。 The if
tag's documentation page描述了已定义变量的边缘情况规则:
The rules to determine if an expression is
true
orfalse
are the same as in PHP; here are the edge cases rules:Value Boolean evaluationempty string falsenumeric zero falsewhitespace-only string trueempty array falsenull falsenon-empty array trueobject true
See TwigFiddle进行演示(标题中“更多选项...”链接后面的 strict_variables
设置为 false
)。
(免责声明:我在 @The_Unknown 指出也可以使用 default
过滤器之前编写了此方法。)
如果将 strict_variables
设置为 false
的想法太笼统,您还可以扩展 Twig。我认为最好将 strict_variables
设置为 true
以避免由例如以下原因引起的意外错误:变量名中存在拼写错误,因此这种方法可能会更好。
我认为您不能创建一个过滤器来执行此操作,因为 undefined variable 仍然会引发异常。您也许能够创建自定义标签、测试或扩展(有关扩展 Twig 的方法,请参阅 Extending Twig);我将创建一个自定义函数,因为它可能是最简单的方法。
$twig->addFunction(new Twig_Function('istruthy', function($context, $var) {
return array_key_exists($var, $context) && $context[$var];
}, ['needs_context' => true]));
['needs_context' => true]
部分在这里至关重要,因为这样您就可以访问 $context
,其中包含当前上下文中存在的变量。 (例如,您可以将 var_dump($context)
放在 return 语句上方以自行查看。)
如果您希望istruthy
支持同时检查多个变量,您可以这样做:
$twig->addFunction(new Twig_Function('istruthy', function($context, ...$vars) {
foreach ($vars as $var) {
if (!array_key_exists($var, $context) || !$context[$var]) {
return false;
}
}
return true;
}, ['needs_context' => true]));
然后在 Twig 中你可以这样做:
{% if istruthy('foo') %} ... {% endif %}
{% if istruthy('foo') or istruthy('bar') %} ... {% endif %}
{# These two are the same: #}
{% if istruthy('foo') and istruthy('bar') and istruthy('baz') %} ... {% endif %}
{% if istruthy('foo', 'bar', 'baz') %} ... {% endif %}
{# Ternary operator can also be used: #}
{{ istruthy('foo') ? 'yep' : 'nope' }}
您可能想在 istruthy
函数中检查参数是字符串还是其他内容,然后采取相应的操作。 array_key_exists
期望第一个参数是字符串或整数。
关于twig - 检查 Twig 中的变量是否已定义并且是否为真,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48302206/
Twig 使用 {{ }}、{% %}、{# #} 分隔符。 但是如何在 Twig 模板中显示 {{ }} 呢?我不是在谈论 HTML 转义。 我问这个问题是因为我想在我的 Twig 模板中包含一个
为了转换我的法语 html 口音,我尝试通过在 Twig 中执行此操作来更改编码: {{ ddf.description | convert_encoding('UTF-8', 'HTML-ENTIT
我有一个带有输出元素的 .twig 文件的循环。 我需要为每个元素增加一个值。我知道如何在 PHP 中执行此操作,但不清楚如何使用 twig 文档执行此操作。我真的不能在 Controller 中做到
我有一个 xxx.html.twig 文件,它显示一个页面,但是当我想用不同的数据刷新页面并用新数据更新它时,我有一个选择和一个提交按钮。问题是我不知道如何在 Controller 中调用一个 Act
我需要使用 Liquid 来创建模板,但可能只能访问 Twig 模板语言。这两种语言基本上是相同的东西,还是语言上存在差异,导致很难使用 Twig 来替代 Liquid? 最佳答案 Twig 和 Li
number rounding 的 Twig 文档谈论四舍五入小数,但我有一个案例,我想将 19,995 这样的数字四舍五入到 20,000。是否有一种巧妙的方法可以四舍五入到最接近的千位? 最佳答案
我在 2.7 和 3.1 版本中都有 Symfony 项目。 PhpStorm 启用了 Symfony 插件和 Twig 支持插件。 当我在 Symfony 2.7 版本的 PhpStorm 中工作时
我已升级到新版本的 twig/twig 3.x.x。从 twig 版本 3.0 开始,Twig_Extension 已被弃用。所以我接下来要讨论“whiteoctober/breadcrumbs-bu
我在 Twig 中经常有类似的样板代码: {% if (somelongcond) %} {% endif %} Some long content {% if (somelongcond)
我想在我自己的插件中覆盖来自插件的 Twig 模板的 block 。我过去曾覆盖过 native 商店软件模板,但我在使用插件模板时遇到了麻烦。 我正在尝试调整的插件是 FroshProductCom
我用带 Twig 的silex; 我创建自定义 form_div_layout 并将其放在 webroot 中(例如) 我这样注册 TwigService 提供者 $app->register
我不太明白 Twig 中的属性函数是如何工作的。有人可以帮我举个例子吗? 我在 SQL 中有一个名为动态的字段。我可以是例如“field27”,但我不知道号码,号码保存在 radio.id 中。我想做
我想知道是否有一种方法可以在 TWIG 中创建多语句 示例:两个单独的语句... {% set foo:bar %} {% set baz:qux %} 合并成一个语句 {% set foo:
我正在尝试使用 Twig i18n 扩展。 据我所知,我需要的文件在这里: https://github.com/fabpot/Twig-extensions/blob/master/lib/Twig
我正在使用 patternlab 的节点版本用 Twig 作为模板引擎。我正在使用 twig,因为我的代码库是用 twig 编写的 - 所以使用 mustache 不是一个选项。 我简单地尝试包含一个
我有以下代码: {% set pageDescription = item.description|length > 197 ? item.description|striptags|trim|sli
我有以下 Twig 代码: {% for likeditem in user.getItemLikes() %} //iterate over each liked items here {%
反正有没有像这样在 Twig 中重复一个块: {% block title %}{% endblock %} | MyBusiness 为了只声明两个块一次?像那样: {% block title
编辑:2016年12月3日 想学习如何向 Twig 添加自定义扩展(过滤器)吗?请参阅this answer的lxg 您是否只需要找到与ucword等效的嫩枝?请参阅this answer的Javie
我有一个简单的浮点数组。我需要将它显示为逗号分隔的字符串。 {{ arr|join(', ') }} 由于过度的微不足道的准确性,这是一个糟糕的解决方案。 {% for val in arr %}
我是一名优秀的程序员,十分优秀!