Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lift (Scala) nested snippets (multiple items per day)

Tags:

scala

lift

Trying to get started with Scala by playing around with the Lift framework and I'm having trouble creating what I imagined would be typify a common scenario: I have a list of days and for each day a list of items for that day (nested lists).

My thought was to take this approach:

<div class="lift:DaySnippet">
    <h1 class="day">Name of Day</h1>
    <ul class="day-items">
        <!-- wanted to have a separate snippet but haven't made it work -->
        <!-- <li class="lift:DayItemSnippet">Item content</li> -->
        <li class="item">
            <span class="name">Name</span>
            <span class="desc">Description</span>
        </li>
    </ul>
</div>

Originally I wasn't going to have the inner snippet but thought that made sense.

So I can define a snippet like this:

class DaySnippet {
    // Ignoring that this is a stupid way to define the data
    val days = ("Monday", ("Item 1", "Item 1 Description") :: Nil) ::
        ("Tuesday", ("Item 2", "Item 2 Description") ::
            ("Item 3", "Item 3 Description") :: Nil) :: Nil;

    def render = {
        // EDIT: Original code was broken, this is what I was trying to show

        "* *" #> days.map { case (day, items) => ".day *" #> day }
    }
}

At any rate, I'm looking for some documents or examples of either nesting snippets and/or how to iterate over nested collections and use CssSels to modify the whole NodeSeq as we go.

I'd be happy to add any additional information that might clarify.

like image 626
Nick Spacek Avatar asked Aug 02 '11 08:08

Nick Spacek


1 Answers

I came up with some code to do what I wanted, but I'm not sure it's optimal so suggestions are welcome:

class DaySnippet {
    // Ignoring that this is a stupid way to define the data
    val days = ("Monday", ("Item 1", "Item 1 Description") :: Nil) ::
        ("Tuesday", ("Item 2", "Item 2 Description") ::
            ("Item 3", "Item 3 Description") :: Nil) :: Nil;

    def render = {
        "* *" #> days.map { case (day, items) =>
            ".day *" #> day & ".item *" #> item.map {
                case (name, desc) =>
                    ".name *" #> name * ".desc *" #> desc
            }
        }
    }
}
like image 146
Nick Spacek Avatar answered Nov 18 '22 14:11

Nick Spacek