Skip to content

Instantly share code, notes, and snippets.

@nietzscheson
Created March 4, 2016 21:39
Show Gist options
  • Save nietzscheson/6742b33de53ea8a37e43 to your computer and use it in GitHub Desktop.
Save nietzscheson/6742b33de53ea8a37e43 to your computer and use it in GitHub Desktop.
Theme for data collection in Symfony forms
{% use "bootstrap_3_layout.html.twig" %}
{%- block collection_widget -%}
{% if prototype is defined %}
{%- set attr = attr|merge({'data-prototype': form_row(prototype) }) -%}
{% endif %}
<div class="panel panel-default">
<div class="panel-body">
<table class="table table-bordered">
<thead>
<tr>
<th></th>
{{ form_label(form) }}
<th></th>
</tr>
</thead>
{%- if form.parent is empty and errors|length > 0 -%}
<tr>
<td colspan="2">{{- form_errors(form) -}}</td>
</tr>
{%- endif -%}
<tbody {{ block('widget_container_attributes') }}>
{% for child in form %}
<tr>
<td>{{ child.vars.value.id }}</td>
{{ form_widget(child) }}
<td class="text-center">
<div class="btn-group">
<button type="button" class="btn btn-default">
<span class="glyphicon glyphicon-trash"></span>
</button>
</div>
</td>
</tr>
{% endfor %}
</tbody>
</table>
<button type="button" class="btn btn-default">
<span class="glyphicon glyphicon-plus-sign"></span> Add
</button>
</div>
</div>
{%- endblock collection_widget -%}
{%- block form_row -%}
{% spaceless %}
<td>
{{- form_errors(form) -}}
{{- form_widget(form) -}}
</td>
{% endspaceless %}
{%- endblock form_row -%}
{%- block form_label -%}
{% if label is not same as(false) -%}
{% if not compound -%}
{% set label_attr = label_attr|merge({'for': id}) %}
{%- endif -%}
{% if required -%}
{% set label_attr = label_attr|merge({'class': (label_attr.class|default('') ~ ' required')|trim}) %}
{%- endif -%}
{% if label is empty -%}
{%- if label_format is not empty -%}
{% set label = label_format|replace({
'%name%': name,
'%id%': id,
}) %}
{%- else -%}
{% set label = name|humanize %}
{%- endif -%}
{%- endif -%}
<th{% for attrname, attrvalue in label_attr %} {{ attrname }}="{{ attrvalue }}"{% endfor %}>{{ translation_domain is same as(false) ? label : label|trans({}, translation_domain) }}</th>
{%- endif -%}
{%- endblock form_label -%}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment