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!
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.
Templates in Symfony are created with Twig: a flexible, fast, and secure template engine.
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.
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 ):
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
You can use |raw filter. http://symfony.com/doc/current/book/templating.html#output-escaping-in-twig
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With