Search code examples
twigsymfony4

Ternary operator won't return HTML


I would like to use the ternary operator of Twig but it shows me the HTML tags as text.

{{ (sender.firstName or sender.lastName) ? "<strong>#{sender.firstName}  #{sender.lastName}</strong>" : '<strong>Unknown</strong>' }}

The result is <strong>My Name</strong> but it should be My Name.


Solution

  • You have to apply the raw as the output is not marked as safe when concatenating html with twig

    {{ (sender.firstName or sender.lastName ? "<strong>#{sender.firstName}  #{sender.lastName}</strong>" : "<strong>Unknown</strong>")|raw }}
    

    demo