symfony2 twig 白名单 html 标签

发布于 2024-12-13 16:50:30 字数 274 浏览 4 评论 0原文

我在 Symfony2 中将一个变量传递给我的 twig 模板,该变量可能包含
gt;
html 标签,我尝试创建一个扩展(函数),但该变量仍然被转义。

如何输出允许
标记的树枝变量?是否有一个简单的解决方案仅允许某些模板中允许的标签白名单?

我搜索过树枝沙箱,但我不确定这是否是我的解决方案。

编辑:我仍然希望对变量进行转义,但只允许
标记。

I pass a variable to my twig template in Symfony2, this variable may contain <br /> html tags, I have tried to create an extension (function), but the variable still gets escaped.

How can I output a twig variable that allows the <br /> tag? Is there a simple solution to just allow a whitelist of allowed tags in certain templates?

I've searched about twig sandboxes, but I'm not sure if that is my solution.

edit: I still want the variable to be escaped, but to allow exclusively the <br /> tag.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

海夕 2024-12-20 16:50:30

实际上,您可以通过以下方式使用本机 PHP 函数 strip_tags:

{{ var|striptags('<br>')|raw }}

您可以使用以下代码允许多个标签:

{{ var|striptags('<br><p>')|raw }}

Actually, you can use native PHP function strip_tags by following:

{{ var|striptags('<br>')|raw }}

you can allow multiple tags with following code:

{{ var|striptags('<br><p>')|raw }}
满栀 2024-12-20 16:50:30

你可以这样做:

{{ text | striptags('<p><b><br') | raw }}

比如,

<br>

不会逃跑

<br> and <br />

<p>

不会逃跑

<p> and </p>

等等。

You can do like that :

{{ text | striptags('<p><b><br') | raw }}

For instance,

<br>

won't escape

<br> and <br />

and

<p>

won't escape

<p> and </p>

etc.

莳間冲淡了誓言ζ 2024-12-20 16:50:30

最初我认为应该可以编写自定义逃生策略,这样你就可以做这样的事情:

{{ var|escape('html-custom') }}

不幸的是事实并非如此。唯一可用的策略是 html 和 js。它们被硬编码在 Twig_Extension_Core 类文件中定义的 twig_escape_filter() 函数中。

看来你唯一的选择是使用新的过滤器编写自定义扩展:

{{ var|raw|customescape }}

这是自定义树枝扩展的示例以及如何在 Symfony 中注册它: Symfony2 Twig 扩展

Initially I thought it should be possible to write custom escaper strategies so you could do something like this:

{{ var|escape('html-custom') }}

Unfortunately it's not the case. Only available strategies are html and js. They're hard coded in the twig_escape_filter() function defined in a Twig_Extension_Core class file.

It seems that your only option is to write custom estension with a new filter:

{{ var|raw|customescape }}

Here's an example of custom twig extension and how to register it in Symfony: Symfony2 Twig extension

梦与时光遇 2024-12-20 16:50:30
{{ var|striptags('<br>')|raw }} 

工作正常,但我不知道如何使用这个树枝过滤器将数组传递给 strip_tags php 函数。

两者

{{ var|striptags(['<br>', '<b>'])|raw }}

都会

{% set allow = ['<br>', '<b>'] %}
{{ var|striptags(allow)|raw }}

在模板渲染期间抛出“数组到字符串转换”异常。

另请注意,strip_tags php 函数不会像“onclick”那样转义 html 属性。

{{ var|striptags('<br>')|raw }} 

works fine, but I don't know how to pass an array to the strip_tags php function with this twig filter.

both

{{ var|striptags(['<br>', '<b>'])|raw }}

and

{% set allow = ['<br>', '<b>'] %}
{{ var|striptags(allow)|raw }}

throw an "Array to string conversion" exception during the rendering of a template.

Be also carefull that strip_tags php function doesn't escape html attribute like "onclick".

柠檬色的秋千 2024-12-20 16:50:30
{{ var|nl2br }}

和/或

{{ var|raw|nl2br }}

nl2br 参考

{{ var|nl2br }}

and/or

{{ var|raw|nl2br }}

nl2br reference

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文