Turn off escaping in Symfony 2 / twig

2019-02-22 02:55发布

问题:

I'm creating a form using the form builder in Symfony. I am adding an attribute into the twig file for my checkboxes which contains an ampersand and pound sign, Symfony is automatically escaping the ampersand which stops it displaying correctly. Is there anyway to turn off escaping on a per case basis in either the twig file or the controller, or switch it off completely in the config?

{{ form_widget(form.checkbox, { 'attr': {'data-icon-checkmark': '󰀦', 'data-icon-checkmark-checked': '󰀧'} }) }}

I have found a few topics on this for 1.X versions of Symfony, but nothing for 2.

Thanks!

回答1:

Probably what you need is the raw tag or filter. Also take a look at the autoescape tag.

To turn autoescaping off globally, set the autoescape option to false in config.yml:

twig:
    # ...
    autoescape: false


回答2:

You can use |raw filter. http://symfony.com/doc/current/book/templating.html#output-escaping-in-twig



标签: symfony twig