scala 玩!框架。模板“包括”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11763912/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Play! framework. template "include"
提问by socksocket
I'm planning my website structure as following:
我计划我的网站结构如下:
- header.scala.html
- XXX
- footer.scala.html
- 标头.scala.html
- XXX
- 页脚.scala.html
now, instead of "xxx" there should be a specific page (i.e. "UsersView.scala.html").
what I need is to include (like with well-known languages) the source of the footer and the
header into the the middle page's code.
现在,应该有一个特定的页面而不是“xxx”(即“UsersView.scala.html”)。
我需要的是将页脚和页眉的来源(与众所周知的语言一样)包含在中间页面的代码中。
so my questions are:
所以我的问题是:
- How do you include a page in another with scala templating?
- Do you think it's a good paradigm for Play! framework based website?
- 你如何使用 Scala 模板在另一个页面中包含一个页面?
- 你认为它是 Play 的一个很好的范例吗!基于框架的网站?
回答by jparkcool
Just call another template like a method. If you want to include footer.scala.html:
只需像方法一样调用另一个模板。如果你想包含footer.scala.html:
@footer()
@footer()
回答by Brian Smith
A common pattern is to create a template that contains the boilerplate, and takes a parameter of type HTML. Let's say:
一种常见的模式是创建一个包含样板的模板,并采用 HTML 类型的参数。让我们说:
main.scala.html
main.scala.html
@(content: HTML)
@header
// boilerplate
@content
// more boilerplate
@footer
In fact, you don't really need to separate out header and footer with this approach.
事实上,您并不真的需要使用这种方法将页眉和页脚分开。
Your UsersView.scala.html then looks like this:
你的 UsersView.scala.html 看起来像这样:
@main {
// all your users page html here.
}
You're wrapping the UsersView with main by passing it in as a parameter.
您通过将其作为参数传入来使用 main 包装 UsersView。
You can see examples of this in the samples
您可以在示例中看到这方面的示例
My usual main template is a little more involved and looks roughly like this:
我常用的主模板稍微复杂一些,大致如下所示:
@(title: String)(headInsert: Html = Html.empty)(content: Html)(implicit user: Option[User] = None)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>@title</title>
// bootstrap stuff here
@headInsert
</head>
<body>
@menu(user)
<div id="mainContainer" class="container">
@content
</div>
</body>
</html>
This way a template can pass in a head insert and title, and make a user available, as well as content of course.
通过这种方式,模板可以传入头部插入和标题,并使用户可用,当然还有内容。
回答by Lincoln
Play provide a very convenient way to help implement that!
Play 提供了一种非常方便的方法来帮助实现这一点!
Layout part from official docs:
来自官方文档的布局部分:
First we have a base.html (that's we call in django -_-)
首先我们有一个base.html(这是我们在django中调用的-_-)
// views/main.scala.html
@(title: String)(content: Html)
<!DOCTYPE html>
<html>
<head>
<title>@title</title>
</head>
<body>
<section class="content">@content</section>
</body>
</html>
How to use the base.html?
如何使用base.html?
@main(title = "Home") {
<h1>Home page</h1>
}
More information here
更多信息在这里

