简体   繁体   中英

Escaping strings inside a Twig extension

I'm building a Twig extension that facilitates one function. This function generates quite a lot of HTML. Some (but not all) of the output of this function needs to be escaped. Therefore, the function is declared as follows:

new \Twig_SimpleFunction(
   'my_function',
   array($this, 'myFunction'),
   array('is_safe' => array('html'))
)

Is there an easy way to use Twig's internal escape function to escape parts of this function's output?

It might help to know I'm inside a Symfony2 environment.

Twig's escaper is defined here: https://github.com/fabpot/Twig/blob/master/lib/Twig/Extension/Core.php .

I guess it should be possible to create an instance of the class and call its method twig_escape_filter() .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM