短if内的原始细枝变量过滤器



我使用短条件来区分记录列表中显示的值。

例如,如果我想要标识符大于100且强调 (<em> </em>)的客户的名称,执行以下操作:

{# Displays the identifier of the client #}
{% set strClient = '<em>' ~ client.name ~ '</em>' %}
{{ Client.id > 100 ? strClient|raw : client.name }}

这里的HTML是由浏览器处理的和客户名显示强调,事实是,如果例如你想显示你的手机,它可以是null或不,如果它不是显示一个强调的消息;我执行以下操作:

{# Displays the customer phone or message if there #}
{{ Client.id > 100 ? client.tel : '<em> Not found </em>' }}

在后一种情况下,HTML标记不被浏览器处理并显示为纯文本,我还尝试了以下方法:

{# Displays the customer phone or message if there #}
{{ Client.id > 100 ? client.tel : '<em> Not found </em>'|raw }}

还有这个

{# Displays the customer phone or message if there #}
{% set strClient = '<em> Not found </em>' %}
{{ Client.id > 100 ? client.tel : strClient|raw }}

与相同的结果,即当是一个字符串,这存储在一个变量或不得到的HTML标签,总是出现,如果它是处理明文

有没有人知道如何得到HTML字符串在树枝被解释?

在使用原始过滤器时,如果需要将整个语句包在括号中以使其工作,

{{ (Client.id > 100 ? client.tel : '<em> Not found </em>')|raw }}

在我对您的示例的测试中,没有一次尝试成功,包括您的第一个示例:

{# Displays the identifier of the client #}
{% set strClient = '<em>' ~ client.name ~ '</em>' %}
{{ Client.id > 100 ? strClient|raw : client.name }}

然而,如果你用{% autoescape false %}{% endautoescape %}包装你的例子,这应该解决这个问题。如果像这样包装,所有的示例都应该像预期的那样显示:

{% autoescape false %}
{# Displays the customer phone or message if there #}
{{ Client.id > 100 ? client.tel : '<em> Not found </em>' }}
{% endautoescape %}

使用自动转义时不需要|raw

最新更新