I came across to a problem regarding Twig's escaping in Symfony2.
The problem
I'm currently using Symfony's form builder to create a form for managing the categories of my project. My current code for creating the form is the following:
$Form
->add('title', 'text', array('label' => 'Title', 'attr' => array('class' => 'span8')))
->add('parent', 'entity', array(
'label' => 'Category',
'attr' => array('class' => 'selectPicker span8'),
'property' => 'indentedTitle',
'empty_value' => ' -- New Category --',
'required' => false,
'class' => 'News\Entity\Category',
'query_builder' => function(EntityRepository $Repository) {
return $Repository->createQueryBuilder('c')
->orderBy('c.root, c.left');
}
))
->add('commit', 'submit', array('label' => 'Save', 'attr' => array('class' => 'btn btn-info')));
The callback that I added in my Entity "indentedTitle" simply adds two lines before the title, depending on the category level in the tree set.
public function getIndentedTitle() {
return str_repeat("--", $this->level) . $this->title;
}
So far everything works fine, except that when I try to add some HTML code to modify a bit the category name that I output in my select list, it get automatically escaped. For example you can see that I added a simple   tag next to the "empty_value" key in my Form Builder. So as a result I get "  -- New Category --" as first option in my select list.
What I tried
Twig autoescape
{% autoescape false %} {{ form_row(form.parent) }} {% endautoescape %}
Twig extension
I tried writing custom Twig extension, with the single purpose of escaping(html_decode) the whole Set of Object I passed to it - still no good. Unfortunately I did not save my code to paste it here, so I will provide a link where another user proposed the same method as I did(it's for JSON actually, but the concept is the same). link to SO answer
So, to put it simple as my final thought - What do I have to do, in order to use some HTML like "strong" or " " in my select list without getting it escaped?
Thanks in advance.
{{ htmlvar|raw }}
the raw will print the html correct – acrobat