java PLAY框架学习笔记--templates

2 篇文章 0 订阅

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;其它方法请参考官方文档

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值