在 Twig 功能扩展中防止输出转义 [英] Prevent output escaping in Twig function extension

查看:114
本文介绍了在 Twig 功能扩展中防止输出转义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个 Twig 扩展:

I have created a Twig extension:

{{ image ("image.png", 200) }}

你好,我知道我能做到

{{ image ("image.png", 200)|raw }}

...但我更喜欢使用 PHP 代码,这样所有内容(来自这个图像"扩展名)都不会被转义.

...but I would prefer to use PHP code so that everything (from this 'image' extension) is not escaped.

我看不出这是可能的.

我知道我可以防止 Twig 中的所有输出转义,但我只是希望这个扩展不要转义输出,以及其他一切都这样做.

I know I can prevent all output escaping in Twig, but I just want this one extension not to escape output, and everything else to do so.

推荐答案

这可以通过添加 an选项在扩展注册.

This can be done by adding an option at the extension registration.

public function getFilters(){
    return array(
        // ...
        'image' => new \Twig_Filter_Method($this, 'imageFilter',
            array('is_safe' => array('html'))
        ),
        // ...
    );
}

这将提供未转义的输入 HTML 并返回未转义的 HTML 输出.如果您需要使用转义的 HTML 输入,请参阅选项 'pre_escaped' =>'html'.

This will provide unescaped input HTML and return unescaped HTML output. If you need to work with escaped HTML input, see the option 'pre_escaped' => 'html'.

这篇关于在 Twig 功能扩展中防止输出转义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆