play的template采用Groovy语言,play中所有的templates均存放在app/views目录。
官方参考文档:templates
1、采用${...}来引用变量,如:<h1>Client ${client.name} </h1>;若client可能为null,则安全的写法为<h1>Client ${client?.name} </h1>
2、采用#{extends .../}来继承其它template;采用#{doLayout}来占位,表示其子模块的填充位置;可以使用#{get .../}与#{set .../}来在父子模板中共享变量。
3、tag使用格式:#{tagName/},常用的tagName有:script, list, title, verbatim
4、采用@{...}(用相对路径)或@@{...}(用绝对路径)来产生url,用于配置href属性,如:<a href="@{Clients.showAccounts(client.id)}">show all accounts</a>
5、采用&{...}来调用conf/messages文档中的国际化参数,如:conf/messages中的clientName=The client name is %s,若要在模板中展示,只需<h1>&{'clientName', client.name}</h1>
6、采用*{...}*来表示注释,解析器不执行此条语句
7、Scripts: %{…}%
A script is a more complicated set of expressions. A script can declare some variables and define some statements. Use the %{…}%
syntax to insert a script.
%{
fullName = client.name.toUpperCase()+' '+client.forname;
}%
<h1>Client ${fullName}</h1>
A script can write dynamic content directly using the out
object:
%{
fullName = client.name.toUpperCase()+' '+client.forname;
out.print('<h1>'+fullName+'</h1>');
}%
You can use a script to create a structure such as an iteration in your template:
<h1>Client ${client.name}</h1>
<ul>
%{
for(account in client.accounts) {
}%
<li>${account}</li>
%{
}
}%
</ul>
Bear in mind that a template is not a place to do complex things. So, use a tag when you can, or move the computations into the controller or the model object.
8、自定义tags:只需在app/views/tags目录下存放myTag.html即创建了一个myTag;其它方法请参考官方文档