简体   繁体   English

玩! 框架。 模板“包括”

[英]Play! framework. template “include”

I'm planning my website structure as following: 我正在计划我的网站结构如下:

  • header.scala.html header.scala.html
  • XXX XXX
  • footer.scala.html footer.scala.html

now, instead of "xxx" there should be a specific page (ie "UsersView.scala.html"). 现在,而不是“xxx”应该有一个特定的页面(即“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. 我需要的是将页脚的来源和标题包含在(如知名语言)中间页面的代码中。

so my questions are: 所以我的问题是:

  1. How do you include a page in another with scala templating? 如何使用scala模板将页面包含在另一个页面中?
  2. Do you think it's a good paradigm for Play! 你认为这是Play的好范例吗? framework based website? 基于框架的网站?

Just call another template like a method. 只需像方法一样调用另一个模板。 If you want to include footer.scala.html : 如果你想包括footer.scala.html

@footer()

A common pattern is to create a template that contains the boilerplate, and takes a parameter of type HTML. 常见的模式是创建包含样板的模板,并采用HTML类型的参数。 Let's say: 让我们说:

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. 您通过将其作为参数传入包装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. 这样,模板可以传入头部插入和标题,并使用户可用,当然还有内容。

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 更多信息在这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM