symfony2 twig whitelist html tags

后端 未结 5 2423
盖世英雄少女心
盖世英雄少女心 2021-02-20 08:23

I pass a variable to my twig template in Symfony2, this variable may contain
html tags, I have tried to create an extension (function), but the variabl

5条回答
  •  时光取名叫无心
    2021-02-20 08:45

    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

提交回复
热议问题