简体   繁体   中英

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.

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. 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 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. I don't think you can make your own form_row function which works two ways, using two different template fragments.

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.

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. I was completely new to Symfony 2 at the time of asking, and didn't even realise this was possible!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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