and*_*ewf 7 scala playframework-2.0
Play Framework 2模板语言非常好用.然而,虽然它的灵感来自于微软的Razor语言,但一个重要的设计决策是不同的:你如何"逃回"HTML.Razor寻找HTML风格的标签,Play 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>
}
}
Run Code Online (Sandbox Code Playgroud)
请注意,其第二个参数是可变数量的部分.似乎没有办法在Play模板语言中调用它.
我创建了一个名为helper的函数Common.section,如下所示:
def section(title: String)(content: Html) = title -> content;
Run Code Online (Sandbox Code Playgroud)
我试过这个:
@()
@import views.Common.section
@structuredpage("Dashboard")(
section("Latest Requests") {
<p>Blah</p>
},
section("Your Details") {
<p>Blah blah</p>
}
)
Run Code Online (Sandbox Code Playgroud)
... 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>
}
}
Run Code Online (Sandbox Code Playgroud)
... type mismatch; found : play.api.templates.Html required: (String, play.api.templates.Html)在第3行给出,即整个外部curley-brace块被解释为模板文档HTML,而不是 Scala代码!
令人沮丧的是,他们似乎并不比官方播放2文档中的一些代码示例巨大的差别,例如:http://www.playframework.org/documentation/2.0/ScalaTemplateUseCases
有任何想法吗?我正在使用Play Framework 2.0.4
这就是您可能正在寻找的内容。但这并不完全是 FP
结构化页面.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>
}
}
}
Run Code Online (Sandbox Code Playgroud)
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>
}
}
Run Code Online (Sandbox Code Playgroud)
切片方法:
def section(title: String)(content: Html)(implicit sections: scala.collection.mutable.MutableList[Pair[String, Html]]) {
sections += title -> content
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1649 次 |
| 最近记录: |