《Play for Java》学习笔记(三)template+Message

说明: 这是本书的第八章内容,由于项目需要,提到前面来看啦~~~O(∩_∩)O

一、模板template的定义

Play中的模板是html代码和Scala代码的混合而成的,其中Scala代码以@开头,一个简单的模板如下:

@(product: List[Product], count: Integer)

@for(product <- products) {

@routes.Products.show(product.ean)">
@product.ean - @product.name


@product.description

}

注解: 如果想用HTML的方式输出变量或表达式的值,而表示为Scala代码,使用@Html(), 如:

// boldName = "world
Hello @Html(boldName)!

二、表达式的范围Scope--in-line expression

1. 这里的"-"是文本,有两个Scala表达式

2. a multi-token statement

3. a multi-statement expression

4. 注解

  • 应该尽量避免如1,2所示的复杂的表达式
  • Scala代码格式参见: http://docs.scala-lang.org/style/scaladoc.html

三、模板的基本功能说明

1、模板参数

@* general template parameters*@
@(customer: models.Customer, orders: List[models.Order])
@* default values for parameters*@
@(title: String = "Home")
@* several parameter groups  *@
@(title:String)(body: Html)

模板参数应写在template的最开头

2. Iterating

  • 一般List的循环结构见上(略)
  • Map的循环
@(eanMap: Map[Long, Product])
......
@for((ean, product) <- eanMap) {
@ean - @product.name
@product.description
}
......
  • Set的循环
@(products: Set[Product])
......
@for((product, i) <- products.zipWithIndex) {
@routes.Products.show(product.ean)"> Product @i - @product.name
@product.description
}
......

 3. reusable blocks

@display(product: models.Product) = {
  @product.getName() ([email protected]())
}

<ul>
@for(product <- products) {
  @display(product)   
} 
>

还可以写成pure code blocks(不知道该如何翻译~~~)

@title(text: String) = @{
  text.split(' ').map(_.capitalize).mkString(" ")
}
 <h1>@title("hello world")h1>

4. reusable values

@defining(user.getFirstName() + " " + user.getLastName()) { fullName =>
  <div>Hello @fullNamediv>
}

使用@defining()

四、模板的模块化

main.scala.html

@(title: String = "Paperclips")(content: Html)   
DOCTYPE html>
<html>
<head>
<title>paperclips.example.comtitle>
<link href="@routes.Assets.at("stylesheets/main.css")" rel="stylesheet">
head>
<body>
    <div id="header">
    <h1>Productsh1>
    div>
        @navigation     
    <div id="content">
        @content      
div> <footer> <p>Copyright ©2012 paperclips.example.comp> footer> body> html>

view.scala.html

@(products: Seq[Product])
@main("Products") {   
<h2>Productsh2>
<ul class="products">
    @for(product <- products) {
    >
        <h3>@product.nameh3>
        <p class="description">@product.descriptionp>
    li>
    }
ul>
}

调用:  views.html.render(content)

navigation.scala.html

@()
<nav class="navbar navbar-inverse navbar-fixed-top" id="navigation">
    <div class="navbar-inner">
        <div class="container">
        <a class="brand" href="@routes.Products.index()">Products Cataloga>
        <ul class="nav">
            <li><a href="@routes.Products.index()">Homea>li>
            <li><a href="@routes.Products.list()">Productsa>li>
            <li><a href="">Contacta>li>
        ul>div>
    div>
nav>

五、Message的使用和国际化(internationalization)

1. 国际化(internationalization)

在conf/application.conf中设置

application.langs="en,en-US,fr"

2. Message

1)messages files文件所在位置——conf/messages.xxx
如果有不同的语言,其文件名分别为conf/messages.fr或conf/messages.en-US

2)定义Message——使用play.i18n.Messages object对象
 String title = Messages.get("home.title")
 也可以指定语言:
 String title = Messages.get(new Lang(Lang.forCode("fr")), "home.title")

3)在模板templates文件中如何使用
   @import play.i18n._
   @Messages.get("key")

4)Message的格式化方法

conf/messages.en的格式化 调用 输出
files.summary=The disk {1} contains {0} file(s)  Messages.get("files.summary", d.files.length, d.name)  
shop.basketcount=Your cart {0,choice,0#is empty|1#has one item|1< has {0} items} 

@Messages("shop.basketcount", 0)


@Messages("shop.basketcount", 1)


@Messages("shop.basketcount", 10)

Your cart is empty.
Your cart has one item.
Your cart has 10 items.

5)Retrieving supported languages from an HTTP request

public static Result index() {
     return ok(request().acceptLanguages());
}

 参考:http://www.playframework.com/documentation/2.2.x/JavaTemplates

转载于:https://www.cnblogs.com/JoannaQ/p/3629823.html

你可能感兴趣的:(《Play for Java》学习笔记(三)template+Message)