简体   繁体   English

在Twig扩展中转义字符串

[英]Escaping strings inside a Twig extension

I'm building a Twig extension that facilitates one function. 我正在构建一个Twig扩展程序,该扩展程序可以实现一个功能。 This function generates quite a lot of HTML. 此函数会生成很多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? 有没有一种简单的方法可以使用Twig的内部escape函数来转义此函数输出的一部分?

It might help to know I'm inside a Symfony2 environment. 知道我在Symfony2环境中可能会有所帮助。

Twig's escaper is defined here: https://github.com/fabpot/Twig/blob/master/lib/Twig/Extension/Core.php . Twig的转义者在此处定义: 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() . 我想应该可以创建该类的实例并调用其方法twig_escape_filter()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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