我想在 play 2 中定义一些模板,将另一个模板作为参数:
@aTemplate(otherTemplate())
我认为这在scala中应该是可能的,对吧?
中的参数定义如何otherTemplate()
?我还应该有一个默认值。我在想这样的事情:
@(template: PlayScalaViewTemplate = defaultTemplate())
谢谢!
我想在 play 2 中定义一些模板,将另一个模板作为参数:
@aTemplate(otherTemplate())
我认为这在scala中应该是可能的,对吧?
中的参数定义如何otherTemplate()
?我还应该有一个默认值。我在想这样的事情:
@(template: PlayScalaViewTemplate = defaultTemplate())
谢谢!
是的你可以。一旦您发现 Play 模板只是函数,这非常简单。
高阶模板(获取简单模板作为参数的模板)如下所示:
highOrder.scala.html:
@(template: Html => Html)
<html>
<head><title>Page</title></head>
<body>
@template {
<p>This is rendered within the template passed as parameter</p>
}
</body>
</html>
所以,如果你有一个简单的子模板,比如
simple.scala.html:
@(content: Html)
<div>
<p>This is the template</p>
@content
</div>
您可以像这样在控制器中应用模板:
def index = Action {
Ok(views.html.higherOrder(html => views.html.simple(html)))
}
结果将是:
<html>
<head><title>Page</title></head>
<body>
<div>
<p>This is the template</p>
<p>This is rendered within the template passed as parameter</p>
</div>
</body>
</html>
因此,Scala 模板最终是函数,因此您可以像函数一样组合它们。