Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom collection templates in symfony2

Tags:

php

twig

symfony

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.

like image 530
Jens Avatar asked Jan 08 '12 22:01

Jens


People also ask

What is a template in Symfony?

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.

How to render multiple templates in Symfony using twig?

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.

What is an extension in Symfony2?

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.

How to avoid messing with Symfony bundle templates?

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.


1 Answers

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 %}
like image 61
Jens Avatar answered Sep 23 '22 18:09

Jens