简洁,富有表达力,流畅:最小化语法字符和击键要求,让你快速,流畅的编写代码。不像大多数模版引擎的语法,你无须明确的中断HTML代码就可嵌入服务器端逻辑。引擎会智能 的为你识别。这是一个真正简洁,富有表达力的语语法,使输入变得干净,快速,愉快。
易于学习:掌握简单的概念就能让你快速产出。它使用你已掌握的Scala和HTML技能。@(customer: Customer, orders: Seq[Order]) <h1>Welcome @customer.name!</h1> <ul> @orders.map { order => <li>@order.title</li> } </ul>
你可以在你的任何Scala代码中,像函数一样调用它:
val html = views.html.Application.index(customer, orders)
Hello @customer.name! ^^^^^^^^^^^^^ Scala code
Hello @(customer.firstName + customer.lastName)! ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Scala Code
Hello @{val name = customer.firstName + customer.lastName; name}! ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Scala Code
My email is bob@@example.com
@(customer: models.Customer, orders: Seq[models.Order])你也可以指定参数默认值:
@(title: String = "Home")或者甚至是参数组:
@(title:String)(body: => Html)甚至一个隐式参数:
@(title: String)(body: => Html)(implicit request: play.api.mvc.Request)
<ul> @for(p <- products) { <li>@p.name ([email protected])</li> } </ul>
<ul> @products.map { p => <li>@p.name ([email protected])</li> } </ul>
@if(items.isEmpty) { <h1>Nothing to display</h1> } else { <h1>@items.size items!</h1> }
@connected match { case models.Admin(name) => { <span class="admin">Connected as admin (@name)</span> } case models.User(name) => { <span>Connected as @name</span> } }
@display(product: models.Product) = { @product.name ([email protected]) } <ul> @products.map { p => @display(product = p) } </ul>
@title(text: String) = @{ text.split(' ').map(_.capitalize).mkString(" ") } <h1>@title("hello world")</h1>
@implicitFieldConstructor = @{ MyFieldConstructor() }
@defining(user.firstName + " " + user.lastName) { fullName => <div>Hello @fullName</div> }
@(customer: models.Customer, orders: Seq[models.Order]) @import utils._ ...
@* *@:
@********************* * This is a comment * *********************@
@************************************* * Home page. * * * * @param msg The message to display * *************************************@ @(msg: String) <h1>@msg</h1>
<p> @Html(article.content) </p>
让我们声明一个 views/main.scala.html 模版,它將作为主要布局模版:
@(title: String)(content: Html) <!DOCTYPE html> <html> <head> <title>@title</title> </head> <body> <section class="content">@content</section> </body> </html>
你看到,该模版需要两个参数:title 和 Html 内容块。我们可以在另一个 views/Application/index.scala.html 中使用它:
@main(title = "Home") { <h1>Home page</h1> }
注意:我们有时命名参数为@main(title = "Home"),有时候为@main("Home")。依你所好,在不同的场景中选择更简洁的形式。
@(title: String)(sidebar: Html)(content: Html) <!DOCTYPE html> <html> <head> <title>@title</title> </head> <body> <section class="sidebar">@sidebar</section> <section class="content">@content</section> </body> </html>
@main("Home") { <h1>Sidebar</h1> } { <h1>Home page</h1> }
另外,我们可以单独声明边栏:
@sidebar = { <h1>Sidebar</h1> } @main("Home")(sidebar) { <h1>Home page</h1> }
@(level: String = "error")(body: (String) => Html) @level match { case "success" => { <p class="success"> @body("green") </p> } case "warning" => { <p class="warning"> @body("orange") </p> } case "error" => { <p class="error"> @body("red") </p> } }
<h1>Home</h1> <div id="side"> @common.sideBar() </div>