play framework 的内置模板标签

内置模板标签
这些都是可用的模板引擎语法的核心除了内置的标签.
有一个单独的 Java 扩展 手册页。
a
The a tag inserts an HTML link to a controller action.
#{a @Application.logout()}Disconnect#{/a} :play写法
呈现为:
<a href="/application/logout">Disconnect</a> :正常html写法
If the action you try to call does not have any route able to invoke it using a GET method, Play
will automatically generate a hidden form that will be submitted on link click using JavaScript.
authenticityToken
呈现包含生成的标记,您可以使用任何形式的隐藏输入的字段。请参阅跨站点请求伪造。
#{authenticityToken /}
呈现为:
<input type="hidden" name="authenticityToken"
value="1c6d92fed96200347f06b7c5e1a3a28fa258ef7c">
高速缓存 cache
缓存标记主体play.cache.CacheAPI,用于标记参数所指定的缓存键。
#{cache 'startTime'}
${new java.util.Date()}
#{/cache}
主体无限期地高速缓存,默认情况下,但您可以使用的参数指定的过期时间。
#{cache 'currentTime', for:'3s'}
${new java.util.Date()}
#{/cache}
doLayout
使用模板继承,此标记插入计算的子-模板的内容。
<!-- common header here -->
<div id="content">
#{doLayout /}
</div>
<!-- common footer here -->
else
Of course used with the if tag.
#{if user}
Connected user is ${user}
#{/if}
#{else}
Please log in
#{/else}
However, you can also use it with the list tag and it will be executed if the list is empty.
#{list items:task, as:'task'}
<li>${task}</li>
#{/list}
#{else}
Nothing to do...
#{/else}
elseif
#{if tasks.size() > 1}
Busy tasklist
#{/if}
#{elseif tasks}
One task on the list
#{/elseif}
#{else}
Nothing to do
#{/else}
As for else, you can use it with the list tag.
error
输出验证错误消息中,如果存在,标签参数所指定的字段。
#{error 'user.name'/}
可以使用可选字段参数使用不同的字段的错误消息。当您希望共享一个常见的错误消息的几个
字段时,这非常有用。
#{error 'contact.street', field:'contact.address'/}
#{error 'contact.city', field:'contact.address'/}
#{error 'contact.country', field:'contact.address'/}
errorClass
输出文本hasError,如果标签参数所指定的字段的验证错误。这是用于设置 CSS 类的错误输
入字段:
<input name="name" class="#{errorClass 'name'/}">
这相当于:
<input name="name" class="${errors.forKey('name') ? 'hasError' : ''}">
errors
循环访问当前的验证错误。
<ul>
#{errors}
<li>${error}</li>
#{/errors}
</ul>
标签定义在其主体中的隐式变量。
• error, the error
• error_index, the error’s index, starting at 1
• error_isLast, true for the last element
• error_isFirst, true for the first element
• error_parity, alternates between odd and even
<table>
<tr><th>#</th><th>Error</th></tr>
#{errors}
<tr class="${error_parity}"><td>${error_index}</td><td>${error}</td></tr>
#{/errors}
</table>
此外可以使用可选字段参数或只是默认参数,筛选只属于某些字段的错误。
<ul>
#{errors 'myField'}
There where errors with the field myField<br />
<li>${error}</li>
#{/errors}
</ul>
extends
使继承另一个模板的模板。
#{extends 'main.html' /}
field
字段标签是一名佣工,基于不重复自己的精神。它的工作方式:
而不是写作这:
The field tag is a helper, based on the spirit of the Don’t Repeat Yourself. It works this way:
Instead of writing this:
<p>
<label>&{'user.name'}</label>
<input type="text" id="user_name" name="user.name" value="${user?.name}"
class="${errors.forKey('user.name') ? 'has_error' : ''}">
<span class="error">${errors.forKey('user.name')}</span>
</p>
您可以只写:
#{field 'user.name'}
<p>
<label>&{field.name}</label>
<input type="text" id="${field.id}" name="${field.name}" value="${field.value}"
class="${field.errorClass}">
<span class="error">${field.error}</span>
</p>
#{/field}
所以你不要再重复user.name。
form
Inserts a form tag. Play will guess the HTTP method from the route, with POST as the default. If
there are both GET and POST routes configured for the URL, the tag will default to using the first
route defined in routes.
• Optional method either POST or GET.
• Optional id attribute sets an ID to the form element.
• Optional enctype attribute sets the form’s data encoding. it defaults to ‘application/x-wwwform-
urlencoded’.
Charset encoding is always utf-8.
#{form @Client.create(), method:'POST', id:'creationForm',
enctype:'multipart/form-data' }
...
#{/form}
呈现为:
<form action="/client/create" id="creationForm" method="POST"
accept-charset="utf-8" enctype="multipart/form-data">
...
</form>
您还可以指定目标实体作为操作方法的一部分:
#{form @Client.update(client.id)}
...
#{/form}
从您在您的操作方法中的声明时检测到 HTTP 参数名称。
public static void update(String clientId){
// ...
}
Play will create an action URL with clientId:
<form action="/client/update?clientId=3442" method="POST"
accept-charset="utf-8" enctype="multipart/form-data">
...
</form>
如果您的表单更新服务器端的资源,您应该使用POST方法。如果您的窗体用来筛选数据,
并不能更新您的域,您可以使用 GET。请阅读关于幂等性。邮政不是幂等,则获取、 传和删
除。If your form updates a resource on the server-side, you should use the POST method. If your
form is used to filter data and does not update your domain, you can use a GET. Please read about
idempotence. POST is not idempotent, whereas GET, PUT and DELETE are.
get
检索一个值,以get标记定义。get/set 机制可用于交换模板、 布局和 sub-templates 之间的值。
<head>
<title>#{get 'title' /}
</head>
您还可以使用获取标记以下列方式,将显示"Homepage",如果不指定标题。
<head>
<title>#{get 'title'}Homepage #{/}
</head>
国际化i18n
出口本地化 JavaScript 中的消息。您使用i18n()函数的 JavaScript 代码中可以使用本地化的消
息。
Define your translations in the conf/messages file.
hello_world=Hello, World !
hello_someone=Hello %s !
Include the messages in your template (or layout) page:
#{i18n /}
和从 JavaScript 检索键:
alert(i18n('hello_world'));
alert(i18n('hello_someone', 'John'));
(可选),可以将标记限定只有一些消息。在结束时接受了通配符字符:
#{i18n keys:['title', 'menu.*'] /}
if
计算给定的测试,并如果为 true,则计算结果标记主体。
#{if user.countryCode == 'en' }
Connected user is ${user}
#{/if}
使用复合条件:
#{if ( request.actionMethod == 'administer' && user.isAdmin() ) }
You are admin, allowed to administer.
#{/if}
ifError
如果命名的标记参数的输入的字段的验证错误,将呈现标记主体。
#{ifError 'user.name'}
<p>
User name is invalid:
#{error 'user.name' /}
</p>
#{/ifError}
ifErrors
如果任何字段的验证错误,将呈现标记主体。
#{ifErrors}
<p>Error(s) found!</p>
#{/ifErrors}
ifnot
Cleaner alternative to #{if !condition}
#{ifnot tasks}
No tasks today
#{/ifnot}
include
包括另一个模板。当前模板变量都直接可以包含的模板中。
<div id="tree">
#{include 'tree.html' /}
</div>
jsAction
# {JsAction /} 标记返回建基于服务器操作和自由变量 URL 的 JavaScript 函数。它不执行 AJA
X 请求 ;这些要做手使用返回的 URL。The #{jsAction /} tag returns a JavaScript function
which constructs a URL based on a server action and free variables. It does not perform an AJAX
request; these have to be done by hand using the returned URL.
让我们看看一个示例:
GET /users/{id} Users.show
现在您可以导入该路线客户端:Now you can import this route client side:
<script type="text/javascript">
var showUserAction = #{jsAction @Users.show(':id') /}
var displayUserDetail = function(userId) {
$('userDetail').load( showUserAction({id: userId}) )
}
</script>
list
对象集合进行迭代。
<ul>
#{list items:products, as:'product'}
<li>${product}</li>
#{/list}
</ul>
标签定义在其主体中的隐式变量。循环变量名称前缀的变量的名称。
• name_index,项的索引,从 1 开始
• name_isLast,真正的最后一个元素
• name_isFirst,真正的第一个元素
• name_parity,时而奇数,甚至
<ul>
#{list items:products, as:'product'}
<span class="${product_parity}">${product_index}. ${product}</span>
${product_isLast ? '' : '-'}
#{/list}
</ul>
The items parameter is optional and can be replaced by the default arg argument.
因此,您可以重写:
#{list items:users, as:'user'}
<li>${user}</li>
#{/list}
作为:
#{list users, as:'user'}
<li>${user}</li>
#{/list}
for loops are easy to create using Groovy range object:
#{list items:0..10, as:'i'}
${i}
#{/list}
#{list items:'a'..'z', as:'letter'}
${letter} ${letter_isLast ? '' : '|' }
#{/list}
以及作为参数是可选的。它使用_作为默认变量名:The as parameter is optional as well. It uses
_ as default variable name:
#{list users}
<li>${_}</li>
#{/list}
option
Insert an option tag in the template.
• value - option’s value
#{option user.id} ${user.name} #{/option}
Will output:
<option value="42">jto</option>
script
Inserts a script tag in the template. By convention, the tag refers to a script in /public/javascripts
• src (required) - script file name, without the leading path /public/javascripts
• id (optional) - an id attribute value for the generated script tag
• charset (optional) - sets source encoding – defaults to UTF-8
The src parameter can be replaced by the default arg argument.
#{script 'jquery-1.4.2.min.js' /}
#{script id:'datepicker' , src:'ui/ui.datepicker.js', charset:'utf-8' /}
呈现render
render标记参数中的路径指定的模板。路径是绝对的或相对于/app/views
#{render 'Application/other.html'/}
select
Insert a select tag in the template.
• name (required) - attribute sets a name to the select element.
Any unknow attribute will be considered as an HTML attribute, and rendered "as is"
#{select 'booking.beds', value:2, id:'select1'}
#{option 1}One king-size bed#{/option}
#{option 2}Two double beds#{/option}
#{option 3}Three beds#{/option}
#{/select}
将输出:
<select name="booking.beds" size="1" id="select1" >
<option value="1">One king-size bed</option>
<option value="2" selected="selected">Two double beds</option>
<option value="3">Three beds</option>
</select>
This tag can generate options using items attribute.
• items (optional) - list of objects, used to create options
• value (optional) - selected element in items
• labelProperty (optional) - for each item, attribute used as option’s label
• valueProperty (optional) - for each item, attribute used as option’s value. id is used by
default
The labelProperty and valueProperty shouldn’t be primitives value. So, for example, instead of
using int or long use an Integer or Long variable.
For example, giving a list of User, each having a name attribute:
#{select 'users', items:users, valueProperty:'id', labelProperty:'name',
value:5, class:'test', id:'select2' /}
将输出:
<select name="users" size="1" class="test" id="select2" >
<option value="0" >User-0</option>
<option value="1" >User-1</option>
<option value="2" >User-2</option>
<option value="3" >User-3</option>
<option value="4" >User-4</option>
<option value="5" selected="selected">User-5</option>
</select>
set
在相同的模板或任何布局与set标记中定义,可以检索到的值。
#{set title:'Admin' /}
#{set style:'2columns' /}
您还可以使用变量:
#{set title:'Profile of ' + user.login /}
在正文中,可以定义变量的值:
#{set 'title'}
Profile of ${user.login}
#{/set}
样式表stylesheet
Inserts a link tag in the template. By convention, the tag refers to a CSS file in /public/stylesheets
• src (required) - file name, without the leading path /public/stylesheets
• id (optional) - an id attribute value for the generated link tag
• media (optional) - a media attribute value: screen, print, aural, projection…
• title (optional) - title attribute value (or description)
The src parameter can be replaced by the default arg argument.
#{stylesheet 'default.css' /}
#{stylesheet id:'main', media:'print', src:'print.css', title:'Print stylesheet'
/}
逐字记录verbatim
禁用 HTML 模板输出,像raw()Java 扩展,但对于整个标记主体中逸出。
${'&amp;'}
#{verbatim}${'&amp;'}#{/verbatim}
在此示例中,第一行输出&amp ;而第二行输出 & 符。In this example, the first line outputs
&amp; while the second line outputs an ampersand.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值