簡體   English   中英

如何在symfony2中使用twig繼承更改標簽模板

[英]how to change a label template using twig inheritance in symfony2

使用帶有Twig的Symfony2.3.​​4。

假設我正在嘗試添加冒號(:),如果需要 ,還可以在Symfony2的CRUD生成的表單中的每個字段的每個標簽上添加一個星號(*)。 為此,我使用twig模板繼承Symfony2的主模板form_div_layout.html.twig

至今:

//config.yml  
twig:
    form:
        resources:
        - ::my_form_layout.html.twig

//my_form_layout.html.twig
{% block form_label %}
    {% spaceless %}
        {% if label is not sameas(false) %}
            ...
            {% set asterisk = '<sup><i title="Campo obligatorio" 
            class="glyphicon-asterisk" style="color: red; font-size: 
            8px"></i></sup>' %}
            <label {% for attrname, attrvalue in label_attr %} 
            {{ attrname }}="{{ attrvalue }}"{% endfor %}>
            {{ label|trans({}, translation_domain) }}: {% if required %}
            {{ asterisk|raw }} {% endif %}
            </label>
            ...
        {% endif %}
    {% endspaceless %}
{% endblock form_label %}

例如,當我渲染一個用於選擇人的性別的選擇類型字段時,問題就是這種方式, 擴展必需設置為TRUE ,冒號(:)和星號(*)出現在單詞Sex AND旁邊男性女性也是如此。

如何使模板在父項和兩個子項之間區分,因此冒號和星號僅在單詞Sex之后出現。

感謝名單

這就是我做類似的事情。 在自定義表單布局中稍微修改“choice_widget_expanded”塊:

// update this row: {{ form_label(child) }}
{{ form_label(child, child, {'exclude_additions': 'true'}) }}

並更新您的'form_label'以檢查是否定義了此值:

{{ label|trans({}, translation_domain) }}{% if exclude_additions is not defined %}: {% if required %}
{{ asterisk|raw }} {% endif %} {% endif %}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM