Twig 在细枝庙宇中显示链接
这看起来很简单,但我遇到了一个奇怪的行为 在细枝文件中:Twig 在细枝庙宇中显示链接,twig,Twig,这看起来很简单,但我遇到了一个奇怪的行为 在细枝文件中: {% set my_html = '<a href="#">Hello world</a>' %} {{- true is not same as(false) ? (true is same as(false) ? ('1'~my_html)|raw : ('2'~my_html)|raw) -}} 它将显示:2 我不明白为什么我需要在其他东西上应用过滤器来获得预期的结果?这是一个bug吗?这不是bug,但由于
{% set my_html = '<a href="#">Hello world</a>' %}
{{- true is not same as(false) ? (true is same as(false) ? ('1'~my_html)|raw : ('2'~my_html)|raw) -}}
它将显示:2
我不明白为什么我需要在其他东西上应用过滤器来获得预期的结果?这是一个bug吗?这不是bug,但由于
twig
的默认设置将自动转义变量。你可以在网站上阅读更多关于它的信息 这是过滤器的记录行为。我引述该页的说明: 使用原始筛选器内部表达式时要小心:
{% autoescape %}
{% set hello = '<strong>Hello</strong>' %}
{% set hola = '<strong>Hola</strong>' %}
{{ false ? '<strong>Hola</strong>' : hello|raw }}
does not render the same as
{{ false ? hola : hello|raw }}
but renders the same as
{{ (false ? hola : hello)|raw }} {% endautoescape %}
我错过了那个文档,真不敢相信:)谢谢,一切都清楚了!
{% autoescape %}
{% set hello = '<strong>Hello</strong>' %}
{% set hola = '<strong>Hola</strong>' %}
{{ false ? '<strong>Hola</strong>' : hello|raw }}
does not render the same as
{{ false ? hola : hello|raw }}
but renders the same as
{{ (false ? hola : hello)|raw }} {% endautoescape %}
{% set my_html = '<' %}
{# ('1'~my_html) is not safe, so the whole expression is not #}
{{ false
? ('1'~my_html)
: ('2'~my_html)|raw
}}
{% set my_html = '<' %}
{# now both strings are safe, so is the whole expression #}
{{ false
? ('1'~my_html)|raw
: ('2'~my_html)|raw
}}