Symfony2 form_label raw

时间:2014-04-29 08:00:25

标签: forms symfony twig

我想渲染原始翻译,所以我决定在twig模板中使用'raw'选项。但它不起作用。例如:

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

在我的网站上,我会看到:

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

我不想看到HTML代码,我想看到链接。如何显示表格的原始标签?

3 个答案:

答案 0 :(得分:6)

在这里阅读:http://symfony.com/doc/current/cookbook/form/form_customization.html

如果您的字段名称为product [name],则可以为单个字段覆盖标签块:

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

或者例如:

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

只需将代码放在您呈现表单的模板中,然后添加

即可
{% form_theme form _self %}

因此渲染引擎将首先在同一文件中搜索覆盖的块

如果使用完整堆栈框架,可以在\ vendor \ symfony \ symfony \ src \ Symfony \ Bridge \ Twig \ Resources \ views \ Form \ form_div_layout.html.twig中找到默认模板文件。

答案 1 :(得分:1)

我也试过这个:http://twig.sensiolabs.org/doc/tags/autoescape.html

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

但它不起作用。为什么?因为当你使用form_label()函数时,Symfony使用\ vendor \ symfony \ symfony \ src \ Symfony \ Bridge \ Twig \ Resources \ views \ Form \ form_div_layout.html.twig和这个块:

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

我可以尝试:

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

但原始选项将在form_div_layout.html.twig中覆盖。我终于决定这样做了:

{{ 'form.roules_acceptance'| trans | raw }}

答案 2 :(得分:0)

由于要保持form_div_layout的行为,因此我结束了以下内容,所以我只是:

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

并且可以使用扩展版本,因此可以正确处理翻译,

{% 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 -%}
相关问题