Symfony 3嵌套集合与Twig模板化

时间:2018-06-27 10:32:27

标签: symfony twig

我该如何模板化嵌套集合?

我用块创建模板父集合:

{% block _group_match_groupMatchType_entry_row %}

在此区块中,我有收藏集:

<div class="js-collection-parrent round text-right" data-prototype="{{ form_row(form.matchResult.vars.prototype)|e('html_attr') }}">

我如何获取具有父集合groupMatchType的matchResult集合的每个条目行?

GroupMatchType

$builder
        ->add('groupMatchType', CollectionType::class, [
            'entry_type' => MatchType::class,
            'allow_add'     => true,
            'allow_delete'  => true,
            'by_reference'  => false
        ])

MatchType

$builder
        ->add('matchResult', CollectionType::class, [
            'entry_type' => MatchResultType::class,
            'allow_add'     => true,
            'allow_delete'  => true,
            'by_reference'  => false
        ])

然后查看

{% block _group_match_groupMatchType_entry_row %}
   <div class="js-collection-parrent round text-right" data-prototype="{{ form_row(form.matchResult.vars.prototype)|e('html_attr') }}"></div>
{% endblock %}

我必须找到上述块的名称(例如_group_match_groupMatchType_entry_row_matchResult_entry_row)

2 个答案:

答案 0 :(得分:1)

好的,我解决了。该块应命名为

_group_match_groupMatchType_entry_matchResult_entry_row

答案 1 :(得分:0)

在树枝上以表单的形式获取所有子元素很简单:

{% for element in form.elements %}
    {# Do something with this element #}
    {# But think that it will represent the form element #}
{% endfor %}

真实示例:

{% for movie in form.movies %}
    {{ form_widget(movie.title, {'attr' : {'class':'form-control'}}) }}
    {{ form_row(movie.id) }}
{% endfor %}

不是很清楚您是否需要什么,但我相信这是您所要表达的一种方法。

相关问题