首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

通译:Play scala模板系统

2012-10-27 
翻译:Play scala模板系统Import statementsImport 语句You can import whatever you want at the begining

翻译:Play scala模板系统
Import statements

Import 语句

You can import whatever you want at the begining of your template (or of a sub template):

你可以在模板或子模板开头import库

@(customer:models.Customer, orders:Seq[models.Order]) @import utils._ …
Composing templates (tags, layouts, includes, etc.)

Templates being simple functions you can compose them in any way you want. Below are a few examples of other common scenarios:

模板是简单的函数,你能用任何任何你想的方式编写它们。下面是一些常见的写法的例子

Layout

Let’s declare a views/main.scala.html template that will act as main layout:

我们来定义一个views/main.scala.html模板作为主layout

@(title:String)(content: => Html) <h1>@title</h1> <hr> <div id="main">    @content</div> <hr> <div id="footer">    ...</div>

As you see this template takes 2 parameters: a title and an HTML block.

这个模板有2个参数,一个标题,一个html块。

Now we can use it from another views/Application/index.scala.html template:

现在我们在另一个叫views/Application/index.scala.html的模板中使用它:

@main(title = "Home") {        <h1>Home page</h1>    }
Tags

Let’s write a simple views/tags/notice.scala.html tag that display an HTML notice:

我们来写一个简单的views/tags/notice.scala.html tag显示一个html的提醒:

@(level:String = "error")(body: (String) => Html) @level match {        case "success" => {        <p => {        <p => {        <p class="error">            @body("red")        </p>    }    }

And let’s use it from any template:

我们可以在任意的模板中使用它

@import views.tags.html._ @notice("error") { color =>    Oops, something is <span style="color:@color">wrong</span>}
Includes

Nothing special, you can just call any other templates:

没什么特别的,你能在一个模板中使用其它的模板

<h1>Home</h1> <div id="side">    @views.common.html.sideBar()</div>

热点排行