Frage

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?

War es hilfreich?

Lösung

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.

Andere Tipps

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 }}

I ended up by the following as I want to keep the behaviour of the form_div_layout so I just:

{%- block form_label -%}
    {% set label %}{{ label|raw }}{% endset %}
    {{ parent() }}
{%- endblock -%}

and that works the extended version looks like this to handle translation and so correctly:

{% use 'form_div_layout.html.twig' %}

{%- block form_label -%}
    {# Set label to raw label #}
    {% if label is not same as (false) %}
        {% set label %}
            {%- if translation_domain is same as (false) -%}
                {{ label|raw }}
            {%- else -%}
                {{- label|trans({}, translation_domain)|raw }}
            {%- endif -%}
        {% endset %}
    {%- endif -%}

    {# Avoid call of translation again %}
    {% set translation_domain = false %}

    {# Call default behaviour from form_div_layout.html.twig #}
    {{- parent() -}}
{%- endblock -%}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top