Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Turn off escaping in Symfony 2 / twig

Tags:

twig

symfony

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!

like image 986
greg Avatar asked Mar 23 '12 20:03

greg


People also ask

What is raw in Twig?

raw. By default, everything in Twig gets escaped when automatic escaping is enabled. If you don't want to escape a variable you'll have to explicitly mark it as safe which you can do by using the raw filter. This only works if the raw filter is the last filter that is applied to the filter.

Does Symfony use Twig?

Templates in Symfony are created with Twig: a flexible, fast, and secure template engine.

What is block in Twig?

Blocks are used for inheritance and act as placeholders and replacements at the same time. They are documented in detail in the documentation for the extends tag. Block names must consist of alphanumeric characters, and underscores. The first char can't be a digit and dashes are not permitted.

What is macro Twig?

Macros are comparable with functions in regular programming languages. They are useful to reuse template fragments to not repeat yourself. Macros are defined in regular templates. Imagine having a generic helper template that define how to render HTML forms via macros (called forms.html ):


2 Answers

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
like image 109
Elnur Abdurrakhimov Avatar answered Oct 13 '22 03:10

Elnur Abdurrakhimov


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

like image 23
Tuong Le Avatar answered Oct 13 '22 04:10

Tuong Le