Passing unescapable variable to Twig template

I need to display unescaped variable in Twig. I am aware of template raw and unescape functions and filters, but it is a bit complicated to use them here.

Is it possible to define variable unescaped on passing params to template?

// e.g
$vars['rawvar'] = new RawTwigParam('<table><tr><td>HTML table</td></tr></table>');
$vars['normalvar'] = 'This & That';

$twig = new Environment($loader, ['debug' => true]);
return $twig->createTemplate($template)->render($vars);

  • No. Why is it complicated to add | raw to your output?

    – 

  • 1

    The template is edited by client. A certain variable should be always rendered raw. Of course, I could find-replace template content and add “|raw” myself 🙂 But I’d prefer something cleaner, if possible.

    – 




  • 1

    You could look at Twig Markup maybe: stackoverflow.com/a/44736733/231316

    – 

  • Does this answer your question? Returning raw HTML from a function without a Twig filter

    – 

Leave a Comment