Is it posible to add custom global templates for items in collection form fields?
I don't want to customize the collection template itself, but the rendering of each object in the collection, for example for adding a specific class or markup to each object contained in the collection.
I have a form with a collection field added like this:
$builder
->add('items', 'collection', array(
'type' => new ItemType(),
'allow_add' => true,
'allow_delete' => true,
'prototype' => true
));
I want to define a twig template to add a "delete" button to each item in the collection (among other things).
I have found there is a 'collection_widget' template to customize collections, buy this is only for the collection itself, not individual items.
NOTE 1: I need to use a global template in order to do this for all collections in all forms, I know I can solve this for each form template, but that's not the point.
NOTE 2: So far I solved this with jquery, adding a class to collection_widget and adding buttons for all it's children with jquery. This works fine for now, but I'm looking for a 100% template solution, without having to do all the jquery handling. Ideally this should also work with the row prototype for adding new items.
A template is the best way to organize and render HTML from inside your application, whether you need to render HTML from a controller or generate the contents of an email. Templates in Symfony are created with Twig: a flexible, fast, and secure template engine.
Now, if you render the layout.html.twig template, Symfony will render the templates/layout.html.twig file. Use the special syntax @ + namespace to refer to the other namespaced templates (e.g. @email/layout.html.twig and @admin/layout.html.twig ). A single Twig namespace can be associated with more than one template directory.
The first part of the extension, (e.g. .html, .css, etc) is the final format that the template will generate. Unlike the engine, which determines how Symfony2 parses the template, this is simply an organizational tactic used in case the same resource needs to be rendered as HTML ( index.html.twig ), XML ( index.xml.twig ), or any other format.
To avoid messing with your own templates, Symfony adds bundle templates under an automatic namespace created after the bundle name. For example, the templates of a bundle called AcmeFooBundle are available under the AcmeFoo namespace.
Finally I found a good solution to this. First, I had to create a collection_widget custom template (copied from the generic form_widget) and inside, instead of calling the form_rows block, I call collection_rows block, which is a customization of form_rows block. Inside the collection_rows block you can customize whatever you want, I just added a custom class for each child.
Here's the two templates:
{% block collection_widget %}
{% spaceless %}
<div {{ block('widget_container_attributes') }}>
{{ block('collection_rows') }}
{{ form_rest(form) }}
</div>
{% endspaceless %}
{% endblock collection_widget %}
{% block collection_rows %}
{% spaceless %}
{{ form_errors(form) }}
{% for child in form %}
{{ form_row(child, {'attr':{'class':'collection-item'}}) }}
{% endfor %}
{% endspaceless %}
{% endblock collection_rows %}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With