Twig:如何在自定义函数中手动转义? [英] Twig: How can I manually escape inside a custom function?
本文介绍了Twig:如何在自定义函数中手动转义?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我编写了自己的输出 HTML 的 Twig 函数,所以我将 is_safe
设置为 html
以便 Twig 知道不会转义我的函数的输出.
I wrote my own Twig function that outputs HTML, so I have is_safe
set to html
so that Twig knows not to escape the output my function.
但是,我的函数需要参数.其中一个参数直接放在输出中.我想在输出它之前转义它.所以看来我只需要能够在输出它之前手动转义选项值.
However, my function takes arguments. One of the arguments is placed in the output directly. I want to escape this before outputting it. So it seems I just need to be able to manually escape the option value before outputting it.
我找不到如何执行此操作.有人知道吗?
I can't find how to do this. Anyone know?
推荐答案
诀窍是存储树枝环境,然后传递给 twig_escape_filter
The trick is to store the twig enviroment which in turn is passed to twig_escape_filter
class TournExtension extends \Twig_Extension
{
protected $env;
public function initRuntime(\Twig_Environment $env)
{
parent::initRuntime($env);
$this->env = $env;
}
protected function escape($string)
{
return twig_escape_filter($this->env,$string);
}
这篇关于Twig:如何在自定义函数中手动转义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文