Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/webpack/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在无线电标签中添加html代码_Php_Symfony_Twig - Fatal编程技术网

Php 在无线电标签中添加html代码

Php 在无线电标签中添加html代码,php,symfony,twig,Php,Symfony,Twig,我正在为小枝的symfony表单与自定义进行斗争 我的问题是在标签中为单选按钮添加html,如25,但它在默认情况下是转义的,我找不到如何取消转义。我使用以下代码 {% block choice_widget %} {% set labels = { 1: { title: '1 ' ~ 'label.credits'|trans ~ ' - <strong>1€</strong>' }, 2: { title: '2 '

我正在为小枝的symfony表单与自定义进行斗争

我的问题是在标签中为单选按钮添加html,如
25
,但它在默认情况下是转义的,我找不到如何取消转义。我使用以下代码

{% block choice_widget %}
    {%
    set labels = {
        1: { title: '1 ' ~ 'label.credits'|trans ~ ' - <strong>1€</strong>' },
        2: { title: '2 ' ~ 'label.credits'|trans ~ ' - <strong>2€</strong>' },
        25: { title: '25 ' ~ 'label.credits'|trans ~ ' - <strong>25€</strong>' },
        }
    %}

    <div {{ block('widget_container_attributes') }}>
        {% for child in form %}
            {% if child.vars.value matches '/^[1|2|25]{1}/' %}
                {% set currentLabel = labels[child.vars.value].title %}
            {% else %}
                {% set currentLabel = '' %}
            {% endif %}
            {{ form_widget(child, {'label': currentLabel}) }}
            {{ form_label(child) }}
            {{ form_errors(child) }}
        {% endfor %}
    </div>
{% endblock %}
{%block choice\u widget%}
{%
设置标签={
1:{title:'1'~'label.credits'| trans~'-1€'},
2:{title:'2'~'label.credits'| trans~'-2€'},
25:{title:'25'~'label.credits'| trans~'-25€'},
}
%}
{表格%中的子项为%1}
{%if child.vars.value匹配'/^[1 | 2 | 25]{1}/'%}
{%set-currentLabel=标签[child.vars.value].title%}
{%else%}
{%set-currentLabel=''%}
{%endif%}
{{form_小部件(子,{'label':currentLabel}}}
{{form_label(child)}
{{form_errors(child)}
{%endfor%}
{%endblock%}
看这里:


您可以在formType中将参数
expanded
设置为true,将参数
multiple
设置为false,以便在html中使用单选按钮。在这种情况下,您需要覆盖
表单标签
块,并将其合并到您自己的主题()中,这适用于bootstrap 4(在名为
bootstrap\u 4\u layout.html.twig
的供应商文件夹中找到该文件,如果bootstrap 3需要该文件,请执行相同的操作,但该文件是
bootstrap\u 3\u layout.html.twig
),请注意我在哪里插入了
标记:

{% block form_label -%}
    {% if label is not same as(false) -%}
        {%- if compound is defined and compound -%}
            {%- set element = 'legend' -%}
            {%- set label_attr = label_attr|merge({class: (label_attr.class|default('') ~ ' col-form-label')|trim}) -%}
        {%- else -%}
            {%- 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 %}><strong>{{ translation_domain is same as(false) ? label : label|trans({}, translation_domain) }}</strong>{% block form_label_errors %}{{- form_errors(form) -}}{% endblock form_label_errors %}</{{ element|default('label') }}>
    {%- else -%}
        {%- if errors|length > 0 -%}
        <div id="{{ id }}_errors" class="mb-2">
            {{- form_errors(form) -}}
        </div>
        {%- endif -%}
    {%- endif -%}
{%- endblock form_label %}
额外(对于checbkox):要为复选框实现这一点,您需要覆盖标签模板,请拉出该复选框的块
{%block checkbox\u radio\u label-%}
来自
bootstrap\u 4\u layout.html.twig
bootstrap\u 3\u layout.html.twig
,然后将其合并到您自己的主题中


额外额外(对于复选框):如果您还需要自定义小部件,请拉出
{%block checkbox\u widget-%}
的块,即从其中移除标签呈现(是的,由于某些内部原因,小部件也会生成标签)这样,即使在复选框上调用
form\u label
,也会输出标签(否则
form\u label
不会输出任何内容)

是的,谢谢,但我的问题是在标签中添加html,而不是添加单选按钮,这我知道;-)你最终发现了吗?谢谢:)不,我没有发现我刚刚解决了一个类似的问题,关于如何为复选框执行此操作,只是发布了一个关于如何解决您的问题的答案-如果您仍然拥有/需要它!希望这有帮助;-)如何区分“行”标签和“小部件”标签?我需要我的实际单选按钮标签有不同的html,然后为我的单选按钮行标签。。。
{% form_theme form _self %}