Symfony2 form_label raw

15,725

Solution 1

reading here: http://symfony.com/doc/current/cookbook/form/form_customization.html

if the name of your field is lets say product[name] you can overwrite the label block just for the individual field:

{% block _product_name_label %}
     <label>{{ label|raw }}</label>
{% endblock %}

or for example:

{% block _product_name_label %}
     <label>Accept the <a href="url_to_pdf">terms</a></label>
{% endblock %}

just put the code in the template where you render the form and add

{% form_theme form _self %}

so the rendering engine will search for overwritten blocks in the same file first

you can find the default template file in \vendor\symfony\symfony\src\Symfony\Bridge\Twig\Resources\views\Form\form_div_layout.html.twig if you use full stack framework.

Solution 2

I have also try this: http://twig.sensiolabs.org/doc/tags/autoescape.html

{% autoescape false %}
    Everything will be outputted as is in this block
{% endautoescape %}

But it doesn't work. Why? Because when you use form_label() function Symfony use \vendor\symfony\symfony\src\Symfony\Bridge\Twig\Resources\views\Form\form_div_layout.html.twig and this block:

    {% block form_label %}
    {% spaceless %}
        {% if label is not sameas(false) %}
            {% if not compound %}
                {% set label_attr = label_attr|merge({'for': id}) %}
            {% endif %}
            {% if required %}
                {% set label_attr = label_attr|merge({'class': (label_attr.class|default('') ~ ' required')|trim}) %}
            {% endif %}
            {% if label is empty %}
                {% set label = name|humanize %}
            {% endif %}
            <label{% for attrname, attrvalue in label_attr %} {{ attrname }}="{{ attrvalue }}"{% endfor %}>{{ label|trans({}, translation_domain) }}</label>
        {% endif %}
    {% endspaceless %}
    {% endblock form_label %}

I can try:

{{ form_label(form.sfGuardUserProfile.roules_acceptance) | raw }}

but raw option will be override in form_div_layout.html.twig. And I finally decided to made this:

{{ 'form.roules_acceptance'| trans | raw }}
Share:
15,725
Purzynski
Author by

Purzynski

Updated on June 04, 2022

Comments

  • Purzynski
    Purzynski almost 2 years

    I want to render raw translation, so I decided to yous 'raw' option in twig template. But it doesn't work. Example:

    {{ form_label(form.sfGuardUserProfile.roules_acceptance) | raw }}
    

    On my website I will see this:

    Accept the <a href="url_to_pdf">terms</a>
    

    And I don't want to see HTML code, I want to see the link. How to show raw label of form?

  • Herr Nentu'
    Herr Nentu' over 9 years
    Not sure I understand what you did?
  • Purzynski
    Purzynski over 9 years
    I edited my post, maybe it will help if not explain me what you do not understand. I will try help.