如何调用该接受播放框架2数目可变参数的个数模板(How to call a template whi

2019-07-05 03:54发布

播放框架2模板语言是相当不错的。 然而,尽管它是微软的剃刀语言“灵感”,一个重要的设计决定是不同的:你如何“逃回”成HTML。 剃刀查找HTML风格的标签和播放2使用某种启发的。

我试图写一个模板,采用HTML的多个“节”,并生成一个标题页,表的内容。 我的“structuredpage.scala.html”看起来是这样的:

@(title: String)(sections: Pair[String,Html]*)

@main(title){
    <nav class="page-links">
        @makeTableOfContents(sections)
    </nav>
    @for(section <- sections){
        <section id="@section._1">
            <h2>@section._1</h2>
            @section._2
        </section>
    }
}

需要注意的是它的第二个参数是区段的数目可变 。 似乎没有要在播放模板语言调用此的一种方式。

我创建了一个名为辅助函数Common.section看起来像这样:

    def section(title: String)(content: Html) = title -> content;

我已经试过这样:

@()
@import views.Common.section

@structuredpage("Dashboard")(
    section("Latest Requests") {
        <p>Blah</p>
    },
    section("Your Details") {
        <p>Blah blah</p>
    }
)

......这给type mismatch; found : scala.xml.Elem required: play.api.templates.Html type mismatch; found : scala.xml.Elem required: play.api.templates.Html在第5行,即, <p>Blah</p>被解释为Scala的,而不是作为模板文件HTML。

还有这个:

@()
@import views.Common.section

@structuredpage("Dashboard"){
    @section("Latest Requests") {
        <p>Blah</p>
    },
    @section("Your Details") {
        <p>Blah blah</p>
    }
}

......这给type mismatch; found : play.api.templates.Html required: (String, play.api.templates.Html) type mismatch; found : play.api.templates.Html required: (String, play.api.templates.Html)第3行,即,整个外柯利撑块被解释为模板文件的HTML,而不是作为Scala代码!

令人沮丧的是,他们似乎并不比官方播放2文档中的一些代码示例巨大的差别,例如: http://www.playframework.org/documentation/2.0/ScalaTemplateUseCases

有任何想法吗? 我使用的播放框架2.0.4

Answer 1:

以下是你可能会寻找。 它不完全FP虽然

structuredpage.scala.html

@(title: String)(content: scala.collection.mutable.MutableList[Pair[String, Html]] => Unit)

@main(title){
    @defining(new scala.collection.mutable.MutableList[Pair[String,Html]]()) { sections =>
        @content(sections)
        @for(section <- sections){
            <section id="@section._1">
                <h2>@section._1</h2>
                @section._2
            </section>
        }
    }
}

frontpage.scala.html

@()

@import views.Common.section

@structuredpage("Front Page") { implicit sections =>
    @section("Section 1") {
        <h1>stuff</h1>
    }

    @section("Section 2") {
        <h1>more stuff</h1>
    }
}

节方法:

def section(title: String)(content: Html)(implicit sections: scala.collection.mutable.MutableList[Pair[String, Html]]) {
    sections += title -> content
}


Answer 2:

我不能在这台机器现在测试这个权利,但以下应工作(无需附配的方法):

@()

@structuredpage("Dashboard"){
   ("Latest Requests", {
        <p>Blah</p>
    }),
   ("Your Details", {
        <p>Blah blah</p>
    })
}


Answer 3:

这里是一个解决办法:

@import views.Common.section

@sec1 = { <p>Blah</p> }

@sec2 = { <p>Blah blah</p> }

@structuredpage("Dashboard")(
    section("Latest Requests")(sec1),
    section("Your Details")(sec2)
)

以前的尝试:

我觉得你的愿望,使得它复杂。 模板应该很简单。 下面是一个简单的选择:

index.scala.html

@structuredpage("Dashboard"){
    @section("Latest Requests") {
        <p>Blah</p>
    }

    @section("Your Details") {
        <p>Blah blah</p>
    }
}

section.scala.html

@(title: String)(content: Html)

<section id="@title">
    <h2>@title</h2>
    @content
</section>

structuredpage.scala.html

@(title: String)(sections: Html)

@main(title){
    <nav class="page-links">
        table-of-contents goes here
    </nav>
    @sections
}

我做了一个要点: https://gist.github.com/4280577 。 所以,你可以检查出来,并用它玩。



文章来源: How to call a template which accepts variable number of args in Play Framework 2