Twig and autoescaping

I am learning Symfony2. I am currently trying to make a form shortcut in a branch template. The label contains an html tag that does not display correctly in my twig file.

Here follows the code fragment in which the form field is created:

$builder->add('zipcode', 'integer', array(
        'label' => '<abbr title="Zone Improvement Plan">CAP</abbr> code',
        ));

In the twig file, I give the field label as follows:

{{ form_label(form.zipcode) }}

I tried raw, escape, e filters, but the results presented on my html page are always a string

 <abbr title="Zone Improvement Plan">CAP</abbr> code

not the corresponding HTML code!

Any suggestion? Thanks in advance!

I later found a solution. The solution is to disable autoescape in the tag block provided by Symfony along the path: symfony / src / Symfony / Bridge / Twig / Resources / views / Form / form_div_layout.html.twig

So, in your twig file, you should put the following lines outside the form: {% form_theme form _self%}

{% block generic_label %}
{% spaceless %}
  {% if required %}
      {% set attr = attr|merge({'class': attr.class|default('') ~ ' required'}) %}
  {% endif %}
  <label{% for attrname,attrvalue in attr %} {{attrname}}="{{attrvalue}}"{% endfor %}>{% autoescape false %}{{ label|trans }}{% endautoescape %}</label>
{% endspaceless %}
{% endblock %}
+3
2

JeanValjean:

{% autoescape false %}{{ form.zipcode.vars.label | trans }}{% endautoescape %}

, :

{% block generic_label %}
    {% spaceless %}
        {% if required %}
            {% set attr = attr|merge({'class': attr.class|default('') ~ ' required'}) %}
        {% endif %}
        <label{% for attrname,attrvalue in attr %} {{attrname}}="{{attrvalue}}"{% endfor %}>
            {% autoescape false %}{{ label|trans }}{% endautoescape %}
        </label>
    {% endspaceless %}
{% endblock %}
+8

, , , , .

, :

{% autoescape false %}{{ label|trans }}{% endautoescape %}

:

{{ label|trans|raw }}

+7

All Articles