Raw variable filter in short if

I am using a short conditional to differentiate between the values โ€‹โ€‹displayed in a list of entries.

For example, if I want the name of customers who have more than 100 IDs to be underlined ( <em> </em>

), do the following:

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

      

Here HTML is rendered by the browser and the client name is displayed underlined . The point is, if you want to show your phone, which can be null or not, and if it doesn't display the message, it is underlined; I am doing the following:

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

      

In the latter case, the HTML tags are not parsed by the browser and are displayed as plain text.I have also tried the following:

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

      

And this one:

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

      

With the same result, i.e. when it's a string it's stored in a variable or gets no HTML tags, which always looks like it was parsed in plain text .

Does anyone know how to get the HTML string in a branch to be interpreted?

+3


source to share


2 answers


When using a raw tenor filter, if you need to wrap the whole statement in parentheses for it to work.



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

+4


source


In my tests with your example, none of the attempts have worked, including your first example:

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

      

However, if you complete your examples with {% autoescape false %}

and {% endautoescape %}

, that should fix the problem. All of your examples should display as expected if wrapped like this:



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

      

No need |raw

when using autoescape.

+2


source







All Articles