簡潔,富有表達力,流暢:最小化語法字符和擊鍵要求,讓你快速,流暢的編寫代碼。不像大多數模版引擎的語法,你無須明確的中斷HTML代碼就可嵌入服務器端邏輯。引擎會智能 的爲你識別。這是一個真正簡潔,富有表達力的語語法,使輸入變得乾淨,快速,愉快。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 ($@p.price)</li> } </ul>
<ul> @products.map { p => <li>@p.name ($@p.price)</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 ($@product.price) } <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 模版,它將做爲主要佈局模版: web
@(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>