Symfony twig how to add class to a form row

前端 未结 4 1536
情歌与酒
情歌与酒 2020-12-28 16:58

I am building a project in Symfony 2.3 using Twig. I want to add a class to the form row block. I am using a form theme file which contains:

{% block form_ro         


        
4条回答
  •  误落风尘
    2020-12-28 17:43

    What I did was simpler (though maybe a bit less clean?).

    Pass the class for the form row via a field's "data" attribute :

    // template.html.twig
    
    {{ form_start(form) }}
        {{ form_row(form.field, {'attr': {'data-row-class': 'my-row-class'} }) }}
    {{ form_end(form) }}
    

    And then handle it in the form theme template this way :

    // form-theme.html.twig
    
    {% block form_row -%}
        {% set row_class = attr['data-row-class'] | default('') %}
        
    {{- form_label(form) -}} {{- form_widget(form) -}} {{- form_errors(form) -}}
    {%- endblock form_row %}

    Which gives this :

提交回复
热议问题