Symfony2/Twig: how to tell the custom twig tag to NOT escape the output Symfony2/Twig: how to tell the custom twig tag to NOT escape the output symfony symfony

Symfony2/Twig: how to tell the custom twig tag to NOT escape the output


The third argument of Twig_Function_Method::__construct() is an array of options for the function. One of these options is is_safe which specifies whether function outputs "safe" HTML/JavaScript code:

public function getFunctions(){    return array(        'thumbnail' => new \Twig_Function_Method($this, 'thumbnail', array(            'is_safe' => array('html')        ))    );}


Crozin's answer is correct but because \Twig_Function_Method is now deprecated you can use \Twig_SimpleFunction as such:

return [    new \Twig_SimpleFunction('thumbnail', [$this, 'thumbnail'], [        'is_safe' => ['html']    ]),];


Twig does it like this:

class Twig_Extension_Escaper extends Twig_Extension{...    public function getFilters()    {        return array(            new Twig_SimpleFilter('raw', 'twig_raw_filter', array('is_safe' => array('all'))),        );    }...}...function twig_raw_filter($string){    return $string;}