Lift(Scala)嵌套代码段(每天多个项目)

时间:2011-08-02 08:36:39

标签: scala lift

尝试通过使用Lift框架开始使用Scala并且我无法创建我想象的典型场景:我有一个日期列表,每天都列出当天的项目列表(嵌套列表)。

我的想法是采取这种方法:

<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>

最初我不会有内部片段,但认为这是有道理的。

所以我可以像这样定义一个片段:

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 }
    }
}

无论如何,我正在寻找一些嵌套片段和/或如何迭代嵌套集合的文档或示例,并使用CssSel来修改整个NodeSeq。

我很乐意添加任何可能澄清的其他信息。

2 个答案:

答案 0 :(得分:6)

我提出了一些代码来做我想做的事情,但我不确定它是否是最佳的,所以欢迎提出建议:

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
            }
        }
    }
}

答案 1 :(得分:0)

这里描述了良好的discussion,使用S.attr获取嵌套代码段的索引。