Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nancy - Super Simple View Engine : Nested @Each

Tags:

nancy

Does SSVE support nested iterators?

I am looking to walk down an object graph in my SSVE view (see below) but I suspect that nested iterators are not the way to go here. Is there another approach?

I know SSVE was intended for internal use and is not meant for full production use, so no problems, but I don't want to add an reference to Razor and mess up my web.config file unless I absolutely have to.

Nested Iterator View (my best guess)

@Master['_Master']

@Section['Content']
    <h1>Assessment - @Model.survey.title</h1>
    @Each.survey.pages 
        <div>
            <h2>@Current.title</h2>
            @Each.questions
                <div>@Current.title</div>
            @EndEach
        </div>
    @EndEach
@EndSection
like image 623
biofractal Avatar asked Mar 30 '12 16:03

biofractal


2 Answers

This question is quite old now, but still crops up as the top search result on Google for nested each using the Super Simple View Engine. In case anyone else runs into this, know that support for Partial Views within an Each has been added to SSVE. (I've used it to do exactly this kind of View nesting - iterating over collections within collections.)

So you can achieve the effect you want using two Views, where the outer one looks like this:

@Master['_Master']

@Section['Content']
    <h1>Assessment - @Model.survey.title</h1>
    @Each.survey.pages 
        <div>
            <h2>@Current.title</h2>
            @Partial['QuestionList', Current.questions]
        </div>
    @EndEach
@EndSection

And then the other View (which SSVE will find with the name QuestionList) looks like this:

@Each
    <div>@Current.title</div>
@EndEach

Though it leads to quite a few files, you should be able to get to arbitrary depth by repeating this pattern.

like image 54
Sergeus Avatar answered Nov 08 '22 13:11

Sergeus


No, that won't work - SSVE is essentially just a regex. It should support a partial inside an each, but currently it doesn't.

like image 45
Steven Robbins Avatar answered Nov 08 '22 12:11

Steven Robbins