lopsided
lopsided

Reputation: 2400

Symfony Twig Form Theming of specific fields

I have a custom form field type and an associated form theme for it. On one page I have a lot of these fields, but one of the fields in particular I want to change.

Is there any way to theme certain fields of the same type (and in the same file) differently?

A simplified example:

form_fields.html.twig: (local theming file)

{% block my_dropdown_row %}
<div>
    {{ form_label(form) }}
    {{ form_widget(form) }}
    {{ form_errors(form) }}
</div>
{% endblock %}

In my form template (all these fields have the same type - my_dropdown

{{ form_row(form.selectionA) }}
{{ form_row(form.selectionB) }}
{{ form_row(form.selectionC) }}
{{ form_row(form.final_selection) }}

How can I style the final field differently to the others? There is a lot of code in these widgets so less duplication the better.

Upvotes: 6

Views: 19463

Answers (2)

Dmitri P
Dmitri P

Reputation: 46

Old question and already answered, but would be nice to have more detailed info.

{% form_theme form _self %}

    {% block _product_name_widget %}
    <div class="text_widget">
        {{ block('form_widget_simple') }}
    </div>
{% endblock %}

{{ form_widget(form.name) }}

Where block name _product_name_widget stands for

_ form type(block prefix)_ form item name_render function

http://symfony.com/doc/current/form/form_customization.html#how-to-customize-an-individual-field

Upvotes: 1

Carlos Granados
Carlos Granados

Reputation: 11351

This can be done. Here is how:

http://symfony.com/doc/current/cookbook/form/form_customization.html

Upvotes: 6

Related Questions