How to add default class to form element when it is timesale Symfony3?

Kind time of day dear!

Learn how to theme forms in Symfony3 / Twig. I want to change the default block form_widget_simpe:
{% block form_widget_simple %}
 {% set type = type|default('text') %}
 <input type="{{ type }}" {{ block('widget_attributes' }} {% if value is not empty %}value="{{ value }}" endif %}>
{% endblock form_widget_simple %}


Here, in the block widget_attributes displays the name, id, required , and other fields.

Please tell me how to set for that block via the parameter "class" by default? Whatever the derivation, among other fields, the default output <input ... class="my-class">
July 2nd 19 at 18:07
1 answer
July 2nd 19 at 18:09
Solution
{%block form_widget_simple -%}
 {%- set type = type|default('text') -%}
 {%- if attr.class is not defined -%}
 {%- set attr = attr|merge({'class': 'my-class'}) -%}
 {%- endif -%}
 <input type="{{ type }}" {{ block('widget_attributes') }} {% if value is not empty %}value="{{ value }}" endif %}>
{%- endblock form_widget_simple -%}


Or if you want to always add the class:

{%block form_widget_simple -%}
 {%- set type = type|default('text') -%}
 {%- set attr = attr|merge({'class': (attr.class|default ~ 'my-class')|trim}) -%}
 <input type="{{ type }}" {{ block('widget_attributes') }} {% if value is not empty %}value="{{ value }}" endif %}>
{%- endblock form_widget_simple -%}

Find more questions by tags SymfonyTwig