Raw filter with ternary operator for html class attribute I've been trying to figure out how to stop Twig escape using the following code, which I feel is a little nicer with the succinct ternary operator.

I hope someone can confirm I'm using the correct syntax and if there are any performance issues doing it like this (as well as alternative methods, if any):

&lt;body{{ bodyClass is defined ? (' class="' ~ bodyClass ~ '"')|raw : '' }}&gt;


It seems to work and is the only way I've managed to get proper output so far!

An alternative version can be seen here, using string interpolation (#{} is my current preferred method):

" class=\"#{bodyClass}\""|raw
Does this work:

&lt;body {{ bodyClass ? class='{bodyClass}'|raw : ''}}&gt;


I don't think you need the brackets and tilde signs...