Scala 如何在Play Framework 2中调用接受可变参数数的模板
PlayFramework2模板语言非常好。然而,尽管它“受到”微软Razor语言的启发,但有一个重要的设计决策是不同的:如何“返回”到HTML。Razor寻找HTML样式的标记,Play2使用某种启发式方法 我正在尝试编写一个模板,它接受多个HTML“部分”,并生成一个包含标题和目录的页面。我的“structuredpage.scala.html”如下所示:Scala 如何在Play Framework 2中调用接受可变参数数的模板,scala,playframework-2.0,Scala,Playframework 2.0,PlayFramework2模板语言非常好。然而,尽管它“受到”微软Razor语言的启发,但有一个重要的设计决策是不同的:如何“返回”到HTML。Razor寻找HTML样式的标记,Play2使用某种启发式方法 我正在尝试编写一个模板,它接受多个HTML“部分”,并生成一个包含标题和目录的页面。我的“structuredpage.scala.html”如下所示: @(title: String)(sections: Pair[String,Html]*) @main(title){ &l
@(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>
}
}
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>
}
)
…导致类型不匹配;在第3行找到:play.api.templates.Html required:(String,play.api.templates.Html)
,即整个外部花括号块被解释为模板文档Html,而不是Scala代码
令人沮丧的是,它们似乎与官方Play 2文档中的一些代码示例差别不大,例如:
有什么想法吗?我正在使用Play Framework 2.0.4我现在无法在这台机器上测试这一点,但以下应该可以工作(不需要辅助方法):
@()
@structuredpage(“仪表板”){
(“最新请求”{
废话
}),
(“您的详细信息”{
废话
})
}
这里有一个解决方法:
@import views.Common.section
@sec1 = { <p>Blah</p> }
@sec2 = { <p>Blah blah</p> }
@structuredpage("Dashboard")(
section("Latest Requests")(sec1),
section("Your Details")(sec2)
)
@import views.Common.section
@sec1={Blah}
@sec2={Blah Blah}
@structuredpage(“仪表板”)(
第1节(“最新请求”)(第1节),
第节(“您的详细信息”)(第2节)
)
以前的尝试:
我想你的愿望让事情变得复杂了。模板应该很简单。以下是一个简单的替代方案:
index.scala.html
@structuredpage("Dashboard"){
@section("Latest Requests") {
<p>Blah</p>
}
@section("Your Details") {
<p>Blah blah</p>
}
}
@(title: String)(content: Html)
<section id="@title">
<h2>@title</h2>
@content
</section>
@(title: String)(sections: Html)
@main(title){
<nav class="page-links">
table-of-contents goes here
</nav>
@sections
}
@(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>
}
}
}
@()
@import views.Common.section
@structuredpage("Front Page") { implicit sections =>
@section("Section 1") {
<h1>stuff</h1>
}
@section("Section 2") {
<h1>more stuff</h1>
}
}
@structuredpage(“仪表板”){
@第节(“最新请求”){
废话
}
@部分(“您的详细信息”){
废话
}
}
section.scala.html
@structuredpage("Dashboard"){
@section("Latest Requests") {
<p>Blah</p>
}
@section("Your Details") {
<p>Blah blah</p>
}
}
@(title: String)(content: Html)
<section id="@title">
<h2>@title</h2>
@content
</section>
@(title: String)(sections: Html)
@main(title){
<nav class="page-links">
table-of-contents goes here
</nav>
@sections
}
@(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>
}
}
}
@()
@import views.Common.section
@structuredpage("Front Page") { implicit sections =>
@section("Section 1") {
<h1>stuff</h1>
}
@section("Section 2") {
<h1>more stuff</h1>
}
}
@(标题:字符串)(内容:Html)
@头衔
@内容
structuredpage.scala.html
@structuredpage("Dashboard"){
@section("Latest Requests") {
<p>Blah</p>
}
@section("Your Details") {
<p>Blah blah</p>
}
}
@(title: String)(content: Html)
<section id="@title">
<h2>@title</h2>
@content
</section>
@(title: String)(sections: Html)
@main(title){
<nav class="page-links">
table-of-contents goes here
</nav>
@sections
}
@(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>
}
}
}
@()
@import views.Common.section
@structuredpage("Front Page") { implicit sections =>
@section("Section 1") {
<h1>stuff</h1>
}
@section("Section 2") {
<h1>more stuff</h1>
}
}
@(标题:字符串)(章节:Html)
@主要(标题){
目录在这里
@部分
}
我提出了一个要点。因此,您可以查看并使用它。以下是您可能要查找的内容。但这并不完全是FP structuredpage.scala.html
@structuredpage("Dashboard"){
@section("Latest Requests") {
<p>Blah</p>
}
@section("Your Details") {
<p>Blah blah</p>
}
}
@(title: String)(content: Html)
<section id="@title">
<h2>@title</h2>
@content
</section>
@(title: String)(sections: Html)
@main(title){
<nav class="page-links">
table-of-contents goes here
</nav>
@sections
}
@(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>
}
}
}
@()
@import views.Common.section
@structuredpage("Front Page") { implicit sections =>
@section("Section 1") {
<h1>stuff</h1>
}
@section("Section 2") {
<h1>more stuff</h1>
}
}
你得到的编译错误是什么?已经添加了编译器错误,以及我对它们的解释。要点是,它要么将太多的模板解释为Scala代码,要么将太少的模板解释为Scala代码!刚刚尝试过,它失败的方式与我的第二个代码示例相同<代码>类型不匹配;找到:play.api.templates.Html必需:(字符串,play.api.templates.Html)在第3行。模板编译器将外部大括号的全部内容解释为HTML模板材料。我将在今晚再次检查原因(当然,避免附加函数/模板的要点)谢谢,但不幸的是,这不好!我的错误是过分简化了最初的问题:我想从“节”列表中动态构建“目录”,所以我不能将“节”作为Html的一块。我同意模板应该尽可能简单。但在这种情况下,很难看到我如何使它变得更简单,并且仍然做我想做的事情。这可能会奏效,谢谢。标题不在内容的旁边,但除此之外,它似乎起到了作用。当我有机会尝试的时候会告诉你的。哈哈!那太卑鄙了,太狡猾了!诚然,这不是很实用,但至少大部分的不愉快都隐藏起来了。它可以工作,并且节标题与它们的内容很接近。我希望Play Framework的人能重新审视一下模板语言中的转义规则,但同时这种方法可能会很有用。