Skip to content

Instantly share code, notes, and snippets.

@broncha
Last active August 29, 2015 14:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save broncha/a8b597ce423242b38a5b to your computer and use it in GitHub Desktop.
Save broncha/a8b597ce423242b38a5b to your computer and use it in GitHub Desktop.
Symfony prototype rendering macro
{% macro widget_prototype(widget, remove_text) %}
{% if widget.vars.prototype %}
{% set form = widget.vars.prototype %}
{% set name = widget.vars.name %}
{% else %}
{% set form = widget %}
{% set name = widget.vars.full_name %}
{% endif %}
<div data-content="{{ name }}">
<a class="btn-remove" data-related="{{ name }}">{{ remove_text }}</a>
{{ form_widget(form) }}
</div>
{% endmacro %}
<div id="post_tags" data-prototype="{{ _self.widget_prototype(form.addresses, 'Remove address')|escape }}">
{% for widget in form.addresses.children %}
{{ _self.widget_prototype(widget, 'Remove address') }}
{% endfor %}
</div>
<a class="btn-add" data-target="post_tags">Add address</a>
$(function(){
$('.btn-add').click(function(event) {
var collectionHolder = $('#' + $(this).attr('data-target'));
var prototype = collectionHolder.attr('data-prototype');
var form = prototype.replace(/__name__/g, collectionHolder.children().length);
collectionHolder.append(form);
return false;
});
$(document).on('click', ".btn-remove", function(event) {
var name = $(this).attr('data-related');
$(this).parent('*[data-content="'+name+'"]').remove();
return false;
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment