Template的语法
Expressions:${…}
声明动态元素,被放置在表达式中
<h1>Client${client.name}</h1>
如果不能确定值是否为空,写法如下:
<h1>Client${client?.name}</h1>
这样只会在client非空时,显示client的名字。
Template decorators:#{extends/}and#{doLayout/}
Decorators为多个模板之间提供共享的页面layout。
用#{get}和#{set}标志在template和decorator间共享变量。
#{extends'simpledesign.html'/}
#{set title:'Adecoratedpage'/}
This content will be decorated.
Tags:#{tagName/}
#{script'jquery.js'/}与#{script'jquery.js'}#{/script}等同
Actions:@{…}or@@{…}
<h1>Client${client.name}</h1>
<p>
<a href="@{Clients.showAccounts(client.id)}"> All accounts</a>
</p>
<hr/>
<a href="@{Clients.index()}">Back</a>
Messages:&{…}
可以用&{…}来显示通用的message
例如:在conf/message中定义
clientName=The client name is%s
在template中用时,可以这么写:
<h1>&{'clientName',client.name}</h1>
Comment:*{…}*
%{
fullName = client.name.toUpperCase()+''+client.forname;
}%
<h1>Client${fullName}</h1>
script写动态内容时,可以用out
%{
fullName = client.name.toUpperCase()+''+client.forname;
out.print('<h1>'+fullName+'</h1>');
}%
也可以用循环
<h1>Client${client.name}</h1>
<ul>
%{
for(accountinclient.accounts){
}%
<li>${account}</li>
%{
}
}%
</ul>
note:不要在模板里写复杂逻辑的东西
Template in heritance
继承其它的模板,如下:
#{extends'main.html'/}
<h1>Some code</h1>
Custom template tags
放在app/views/tags中
如要创建hello标签,只需要创建app/views/tags/hello.html文件
Hello from tag!
使用方法
#{hello/}
Retrieve tag parameters
Hello${_name}!
#{hello name:'Bob'/}
如果只有一个参数,可以这么写
Hello${_arg}!
#{hello'Bob'/}
Invoke tag body
Hello#{doBody/}!
#{hello}
Bob
Format-specific tags
play会根据tag的类型来设置它的类型