我希望进行原始翻译,因此在twig模板中决定使用“raw”选项。但是它不起作用。例如:
{{ form_label(form.sfGuardUserProfile.roules_acceptance) | raw }}
在我的网站上,我将看到这个:
Accept the <a href="url_to_pdf">terms</a>
我不想看到 HTML 代码,而是想看到链接。如何显示表单的原始标签?
阅读此处: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 中找到默认模板文件。
我也尝试过这个:http://twig.sensiolabs.org/doc/tags/autoescape.html
{% autoescape false %}
Everything will be outputted as is in this block
{% endautoescape %}
{% 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.roules_acceptance'| trans | raw }}
由于我想保留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 -%}
form_label
函数? - ferdynator