symfony2 中的自定义集合模板 [英] Custom collection templates in symfony2
问题描述
是否可以为集合表单字段中的项目添加自定义全局模板?
我不想自定义集合模板本身,而是要自定义集合中每个对象的呈现,例如为集合中包含的每个对象添加特定的类或标记.
我有一个表单,其中添加了一个集合字段,如下所示:
$builder->add('items', 'collection', array('类型' =>新项目类型(),'allow_add' =>真的,'allow_delete' =>真的,'原型' =>真的));
我想定义一个树枝模板,为集合中的每个项目添加一个删除"按钮(除其他外).
我发现有一个collection_widget"模板可以自定义集合,购买此模板仅用于集合本身,而不是单个项目.
注意 1:我需要使用全局模板来为所有表单中的所有集合执行此操作,我知道我可以为每个表单模板解决此问题,但这不是重点.
注意 2:到目前为止,我使用 jquery 解决了这个问题,向 collection_widget 添加了一个类,并使用 jquery 为其所有子级添加按钮.这现在工作正常,但我正在寻找一个 100% 的模板解决方案,而不必进行所有的 jquery 处理.理想情况下,这也适用于添加新项目的行原型.
最后我找到了一个很好的解决方案.首先,我必须创建一个 collection_widget 自定义模板(从通用 form_widget 复制)并在内部调用 collection_rows 块,而不是调用 form_rows 块,它是 form_rows 块的自定义.在 collection_rows 块中,您可以自定义任何您想要的内容,我只是为每个孩子添加了一个自定义类.
这是两个模板:
{% 块 collection_widget %}{% 无空间 %}<div {{ block('widget_container_attributes') }}>{{ 块('collection_rows')}}{{ form_rest(form) }}
{% endspaceless %}{% endblock collection_widget %}{% 块 collection_rows %}{% 无空间 %}{{ form_errors(form) }}{% for child in form %}{{ form_row(child, {'attr':{'class':'collection-item'}}) }}{% 结束为 %}{% endspaceless %}{% endblock collection_rows %}
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.
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 %}
这篇关于symfony2 中的自定义集合模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!