简体   繁体   English

在Symfony 2中收集主题,对于每个级别的嵌套表单都不同

[英]Collection theming in Symfony 2, different for each level of nested form

My research into this has discovered various ways of theming Symfony 2 form collections, but so far not one which allows you to distinguish between the form_rows of several nested forms, to have them be displayed differently. 我对此的研究发现了Symfony 2表单集合的各种方法,但到目前为止还没有一个允许你区分几个嵌套表单的form_rows,以使它们以不同的方式显示。

I have a 3-layer nested form, consisting of a top Main, which contains several (DB-backed) Sections in a Collection, each Section containing several (DB-backed) Questions in a Collection. 我有一个3层嵌套表单,由一个顶部Main组成,它包含一个Collection中的几个(DB支持的)Sections,每个Section包含一个Collection中的几个(DB支持的)问题。 I want to use DIVs to control the layout of my Sections, but I want to use TABLEs to control the layout of each set of Questions. 我想使用DIV来控制我的Sections的布局,但是我想使用TABLE来控制每组Question的布局。

<div id="main">
    <div id="section1">
        <table>
            <tr>Question 1</tr>
            <tr>Question 2</tr>
        </table>
    </div>
    <div id="section2">
        <table>
            <tr>Question 3</tr>
            <tr>Question 4</tr>
        </table>
    </div>
</div>

I understand how, for a single entire form, I can change the template fragment used by the form_row function to render the rows, but here I want at least two different types of row. 我理解,对于单个整个表单,我可以更改form_row函数用于呈现行的模板片段,但在这里我想要至少两种不同类型的行。 I don't think you can make your own form_row function which works two ways, using two different template fragments. 我不认为你可以使用两个不同的模板片段制作自己的form_row函数,它有两种工作方式。

How can I render one level of fields completely differently from another? 如何使一个级别的字段与另一个字段完全不同?

So, rather than leave this hanging I'll answer it. 所以,不要把这个挂起来,我会回答它。

What I was asking may not be technically impossible, maybe via @Chausser's proposed nesting detection technique, but it's at least difficult/unwieldy. 我所问的可能在技术上不可能,也许是通过@ Chausser提出的嵌套检测技术,但它至少是困难/笨拙的。

In the end I solved the problem by simply using Symfony's normal customised Form rendering features - adding each field etc into exactly the right place within a detailed Twig template. 最后,我通过简单地使用Symfony的常规自定义表单呈现功能解决了这个问题 - 将每个字段等添加到详细的Twig模板中的正确位置。 I was completely new to Symfony 2 at the time of asking, and didn't even realise this was possible! 在询问时我对Symfony 2全新,甚至没有意识到这是可能的!

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

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