Skip to content

Instantly share code, notes, and snippets.

@eikes
Forked from cowlby/collection_widget.twig
Last active October 18, 2017 13:54
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eikes/5788231 to your computer and use it in GitHub Desktop.
Save eikes/5788231 to your computer and use it in GitHub Desktop.
{% block collection_widget %}
{% spaceless %}
<div class="collection">
{% if prototype is defined %}
{% set attr = attr|merge({'data-prototype': block('collection_item_widget') }) %}
{% endif %}
<div {{ block('widget_container_attributes') }}>
{{ form_errors(form) }}
<ul>
{% for prototype in form %}
block('collection_item_widget')
{% endfor %}
</ul>
{{ form_rest(form) }}
</div>
<div class="clear"></div>
<a class="add" title="Add" href="javascript:void()">
<div style="display: none;"></div>
<span>Add</span>
</a>
</div>
<div class="clear"></div>
{% endspaceless %}
{% endblock collection_widget %}
{% block collection_item_widget %}
{% spaceless %}
<li>
{% set fieldNum = 1 %}
{% for row in prototype %}
<div class="field{{ fieldNum }}">
{{ form_label(row) }}
{{ form_widget(row, { 'attr': { 'class': 'test' }}) }}
</div>
{% set fieldNum = fieldNum + 1 %}
{% endfor %}
<a class="remove" title="Remove" href="javascript:void()">
<span>Delete</span>
</a>
<div class="clear"></div>
</li>
{% endspaceless %}
{% endblock collection_item_widget %}
@b-ma
Copy link

b-ma commented Aug 5, 2013

Hey, thx for this !
just to note you have a small typo line 11 :

block('collection_item_widget')

should be :

{{ block('collection_item_widget') }}

@ujaladh
Copy link

ujaladh commented Mar 3, 2014

I am trying to use collection widget for tag collection in one of the Task/Tag tutorial of Symfony. I am trying to use this in below form for tag collection. I am new to Symfony, and having difficulties getting hold of theme customization. Can you please help me how can I use collection_widget in example form below. Thank You.

<\form action="{{ path('TaskBundle_task') }}" method="POST" {{ form_enctype(form) }}>

{{ form_row(form.title) }}    
{{ form_row(form.description) }}

        <\h3>Tags</h3>
        <ul class="tags" id="tags" data-prototype="{{ form_widget(form.tags.vars.prototype)|e }}">

    {% for tag in form.tags %}
             <li>{{ form_row(tag.name) }} </li>
    {% endfor %}
        </ul>
{{ form_rest(form) }}
        <\input type="submit" value="Submit" />

</form>

@utkarsh2k2
Copy link

Do I need to define javascript:void(). What does it stands for?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment