他のテンプレートをパラメーターとして取る play 2 でいくつかのテンプレートを定義したいと思います。
@aTemplate(otherTemplate())
それはscalaで可能であるべきだと思いますよね?
のパラメータ定義はどのようになりますotherTemplate()
か? また、デフォルト値も必要です。私はそのようなことを考えています:
@(template: PlayScalaViewTemplate = defaultTemplate())
ありがとう!
他のテンプレートをパラメーターとして取る play 2 でいくつかのテンプレートを定義したいと思います。
@aTemplate(otherTemplate())
それはscalaで可能であるべきだと思いますよね?
のパラメータ定義はどのようになりますotherTemplate()
か? また、デフォルト値も必要です。私はそのようなことを考えています:
@(template: PlayScalaViewTemplate = defaultTemplate())
ありがとう!
はい、できます。Play テンプレートが単なる関数であることが分かれば、非常に簡単です。
上位のテンプレート (単純なテンプレートをパラメーターとして取得するテンプレート) は、次のようになります。
higherOrder.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 テンプレートは最終的には関数なので、関数のように構成できます。