简体   繁体   English

集合形式主题symfony2

[英]collection form theming symfony2

I use the following abstract type in my symfony 2 application in order to edit an album 我在symfony 2应用程序中使用以下抽象类型来编辑专辑

    public function buildForm(FormBuilderInterface $builder, array $options)
  {
    $builder
      ->add('title', 'text')
      ->add('description', 'textarea')
      ->add('public', 'checkbox')
      ->add('lists', 'collection', array(
            'attr' => array('data-category' => 'access-right'),
            'type' => 'albumListType',
            'allow_add' => true,
            'allow_delete' => true,
            'by_reference'  => false,
            )
      )
      ->add('medias', 'collection', array(
            'attr' => array('data-category' => 'media'),
            'type' => new MediaType(),
            'allow_add' => true,
            'allow_delete' => true,
            'by_reference'  => false,
            )
        )
    ;
  }

when i display this form i need to display a thumbnail for each medias linked with the article so i try to overload the mediawidget in order to produce the following html 当我显示此表单时,我需要为与该文章链接的每种媒体显示一个缩略图,因此我尝试重载mediawidget以便生成以下html

<div class="row" id="albumtype_media" data-prototype="prototype">
    <div class="col-md-3" id="albumtype_media_0">
            <img src="path"/>
            <input type="hidden" name="albymtype_media[0][path]" value="path"/>
    </div>
    <div class="col-md-3" id="albumtype_media_1">
            <img src="path"/>
            <input type="hidden" name="albymtype_media[1][path]" value="path"/>
    </div>
</div>

in order to achieve this i use a special form theme to customize the media widget 为了实现这一点,我使用特殊的表单主题来自定义媒体小部件

{% block _medias_widget %}
    {% spaceless %}
        {% if prototype is defined %}
            {% set attr = attr|merge({'data-prototype': form_row(prototype) }) %}
        {% endif %}
        <div class="row" {{ block('widget_container_attributes') }}>
            {{ block('collection_media_rows') }}
            {{ form_rest(form) }}
        </div>
    {% endspaceless %}
{% endblock %}

{% block collection_media_rows %}
{% spaceless %}
{{ form_errors(form) }}
{% for innerform in form %}
        {% spaceless %}
             <div class="col-md-3" {{ block('widget_attributes') }}>
                {% if form.parent is empty %}
                    {{ form_errors(innerform) }}
                {% endif %}
                <img src="{{ innerform.vars.value.path }}"/>
                {% for child in innerform %}
                    {{ form_errors(child) }}
                    {{ form_label(child) }}
                    {{ form_widget(child) }}
                {% endfor %}
            </div>
        {% endspaceless %}
{% endfor %}
{% endspaceless %}
{% endblock %}

wichh give me the following output 给我以下输出

<div class="row" id="albumtype_media" data-prototype="prototype">
    <div class="col-md-3" id="albumtype_media" data-prototype="prototype">
            <img src="path"/>
            <input type="hidden" name="albymtype_media[0][path]" value="path"/>
    </div>
    <div class="col-md-3" id="albumtype_media" data-prototype="prototype">
        <div id="albumtype_media_1">
            <input type="hidden" name="albymtype_media[1][path]" value="path"/>
    </div>
</div>

as i begin with symfony 2 i don't have a very good understanding at twig and looking at the source code for form_widget doesn't help me neither so if you could explain me how to get the desired result i would be very greatful. 当我从symfony 2开始时,我对树枝没有很好的理解,而查看form_widget的源代码也无济于事,所以如果您能向我解释如何获得所需的结果,我将非常感激。

Does your twig template inherit from another template? 您的树枝模板是否从另一个模板继承? If not , you should be mindful of block order. 如果不是,则应注意阻止顺序。 Block that are meant to be nested when rendered should be nested. 渲染时应嵌套的块应嵌套。 You cannot define a block as using another block then define that second block afterward. 您不能将一个块定义为使用另一个块,然后再定义第二个块。 block "collection_media_rows" should be defined within block "_medias_widget"... 应该在“ _medias_widget”块中定义“ collection_media_rows”块...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM