Finglish Finglish - 1 month ago 7
Twig Question

can you set is_safe for a global in a twig extension

Twigs documentation for extensions show that it is possible to use "is_safe" with both simple_filters and and simple_functions, to prevent escaping of html tags in returned values, but I can see any examples of using is_safe with globals. Is there a way to do this?

Answer

If your global is pure HTML that needs to be rendered like HTML you could mark it as safe by using

$twig->addGlobal('my_html', new Twig_Markup($html, 'UTF-8'));

If the global is an object and returns the HTML you wrap your return value with a new Twig_Markup

Comments