Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony2 form_label raw

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?

like image 222
Purzynski Avatar asked Apr 29 '14 08:04

Purzynski


2 Answers

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.

like image 151
Bartłomiej Wach Avatar answered Nov 15 '22 07:11

Bartłomiej Wach


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 }}
like image 27
Purzynski Avatar answered Nov 15 '22 07:11

Purzynski