在 choiceType
字段中我有参数 - 选择标签
'choice_label' => function ($pay, $key, $index) {
return "<b>".$pay->getName()."</b><br />";
},
并在 twig 中渲染字段的函数:
{{form_widget(field)}}
{{form_errors(field)}}
当然我想用粗体字渲染getName
,
我尝试使用 twig autoescape
和 {{form_widget(field)|raw}}
- 没有成功
最佳答案
您必须在自定义主题中自定义 choice_label 表单(例如:radioHTML.html.twig)=> 我只是将 |raw 添加到标签文本
{%- block form_label -%}
{% if label is not same as(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 -%}
{%- if label_format is not empty -%}
{% set label = label_format|replace({
'%name%': name,
'%id%': id,
}) %}
{%- else -%}
{% set label = name|humanize %}
{%- endif -%}
{%- endif -%}
<{{ element|default('label') }}{% if label_attr %}{% with { attr: label_attr } %}{{ block('attributes') }}{% endwith %}{% endif %}>
{%- if translation_domain is same as(false) -%}
{{- label|raw -}}
{%- else -%}
{{- label|trans({}, translation_domain)|raw -}}
{%- endif -%}
</{{ element|default('label') }}>
{%- endif -%}
{%- endblock form_label -%}
您的表单类型只是在 choice_label 选项中返回 html 字符串:
$builder->add('fieldName', EntityType::class, array(
'required' => true,
'class' => 'AppBundle\EntityName',
'choice_label' => function (EntityName $entity) {
$html = '<div>';
$html .= $entity->getName();
$html .= '</div>';
return $html;
},
'data' => null,
'multiple' => false,
'expanded' => true)
然后将它用于您的特定领域,就像在您的 Twig TPL 中那样:
{% form_theme from.fieldNalme 'form/radioHTML.html.twig' %}
关于forms - 标签选择表单中的 HTML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45562135/