Skip to content

Add support for nested key paths in loops

Pre-release
Pre-release
Compare
Choose a tag to compare
@tanner0101 tanner0101 released this 12 Feb 21:33
14014a1

Added support for using nested paths when specifying the right-hand expression of a for loop in Leaf templates.

For example with the following code:

struct Show: Content {
    let title: String
    let cast: [CastMember]
    
    struct CastMember: Content {
        let name: String
        let characterName: String
    }
}

let thirthyRock = Show(title: "30 Rock", cast: [
    Show.CastMember(name: "Tina Fey", characterName: "Liz Lemon"),
    Show.CastMember(name: "Alec Baldwin", characterName: "Jack Donaghy"),
    ...
])

...
req.view.render("ShowInfo", [thirthyRock])
...

It is now possible to use a Leaf template such as:

#for(show in shows):
    <h2>#(show.title)</h2>
    <h4>Cast</h2>
    #for(castMember in show.cast):
        <p>#(castMember.name) as #(castMember.characterName)</p>
    #endfor
#endfor