PlayRythm Reference
Built-in Tags
_m
Lookup message string.
@_m("validation.required")
The above has the same effect of the following groovy template code:
&('validation.required')
You can also pass parameters:
@_m("some.message", "param1")
_u
Reverse lookup URL.
@_u(Application.index)
The above code is equal to the following groovy template code:
@{Applicaiton.index}
You can also reverse lookup static url
@_u(/public/javascripts/myscript.js)
args
The args tag declare variables used in the template.
@args String var1, int var2, ...;
args is one of few tags that are not invoked in @tag(...) style. At the moment please use “;” to terminate args statement. This limitation will be removed in the future, and line-break could be an natural way to terminate the statement
You can have any arbitrary number of args statement in any where of your template, all variables declared will be effective in your template.
authenticityToken
This is a clone of the authenticityToken tag in Play’s groovy template.
@authenticityToken()
Rendered as:
<input type="hidden" name="authenticityToken"
value="1c6d92fed96200347f06b7c5e1a3a28fa258ef7c">
authenticityTokenValue
Use authenticityTokenValue when you want to put the authenticity token value inside a javascript code in your view:
$.post("@_u(User.save())",
{authenticityToken: '@authenticityTokenValue()', ...},
function(data){...});
Rendered as:
$.post("/user/save",
{authenticityToken: "1c6d92fed96200347f06b7c5e1a3a28fa258ef7c", ...},
function(data){...});
cache
Caches the tag body using the play.cache.Cache API, for the cache key specified by the tag parameter/
@cache("startTime") {
@(new java.util.Date())
}
The body is cached indefinitely by default, but you can specify an expiration duration with the for parameter.
@cache("currentTime", "3s") {
@(new java.util.Date())
}
cacheOnProd
Same as cache, but cache happens only when play.Play.mode is "prod".
doLayout
Used with template inheritance, this tag inserts the evaluated sub-template’s contents.
<!-- common header here -->
<div id="content">
@doLayout()
</div>
<!-- common footer here -->
doLayout tag has an alias called renderBody
See also template inheritance
error
Outputs the validation error message, if present, for the field specified by the tag parameter.
@error("user.name")
You can use the optional parameter to use a different field’s error message. This is useful when you want several fields to share a common error message.
@error("contact.street", "contact.address")
@error("contact.city", "contact.address")
@error("contact.country", "contact.address")
errorClass
Outputs the text hasError if there is a validation error for the field specified by the tag parameter. This is useful for setting a CSS class for input fields with errors:
<input name="name" class="@errorClass("name")">
which is equivalent to:
<input name="name" class="@(play.data.validation.Validation.hasError("name") ? "hasError" : '')">
errorList
Corresponding to Groovy template’s errors. Iterates over the current validation errors.
<ul>
@errors() {
<li>@error</li>
}
The tag defines implicit variables in its body.
error, the errorerror_index, the error’s index, starting at 1error_isLast, true for the last elementerror_isFirst, true for the first elementerror_parity, alternates betweenoddandeven
<table>
<tr><th>#</th><th>Error</th></tr>
@errorList() {
<tr class="@error_parity"><td>@error_index</td><td>@error</td></tr>
}
</table>
You can also use the optional field parameter, or just the default parameter, to filter only the errors belonging to a certain field.
<ul>
@errorList("myField") {
There where errors with the field myField<br />
<li>@error</li>
}
</ul>
extends
Makes the template inherit another template.
@extends(/main.html)
or
@extends("/main.html")
You can pass parameter to extended template if the there are arguments declared.
Suppose you have a parent template defined as:
@args String style;
<div class="panel @style">
@renderBody()
</div>
And in your extending template, you pass the argument style as:
@extends(panel.html, style: "span4")
or pass by position:
@extends(panel.html, "span4")
debug
You can use debug tag to output debug message to your log file or screen:
@debug("this is a debug message for %s", myvar)
for
Use for to iterate a collection of objects. The synatx is the same as for loop in Java language:
<ul>
@for (Product product: products) {
<li>@product</li>
}
The tag defines implicit variables in it’s body. The variable names are prefixed with loop variable name:
name_index, the item’s index, start with 1name_isFirst, true for the first elementname_isLast, true for the last elementname_parity, alternates betweenoddandevenname_size, the size of the collection
<ul>
@for (Product product: products) {
<span class="@product_parity">@product_index. @product</span>
@(product_isLast ? "" : "-")
}
</ul>
get
Retrieves value defined with a set tag. You may use the get/set mechanism to exchange values between templates, layouts and sub-templates.
<head>
<title>@get("title")</title>
</head>
You can provide a default value in the following way, which will display “Homepage”, if title has not been specified:
<head>
<title>@get("title": "Homepage")</title>
</head>
if-elseif
Evaluates a given test, and if true, evaluate the tag body. The syntax is the same as if clause in Java language
@if ("en".equals(user.countryCode)) {
Connect user is @user
}
You can also use else clause:
@if (null != user) {
Connected user is @user
} else {
Please log in
}
or else if clause
@if (tasks.size() > 1) {
Busy tasklist
} else if (tasks.size() > 0) {
One task in the list
} else {
Nothing to do
}
renderBody
renderBody is an alias of doLayout.
renderSection
Used with template inheritance, this tag inserts the evaluated sub-template’s contents defined with section tag
<div id="sidebar">
@renderSection("sidebar")
</div>
section
Define a block of template to be output in parent template’s named section.
@section sidebar {
<ul>
<li><a href="#">Link one</li>
<li><a href="#">Link two</li>
</ul>
}
set
Define a value which can be retrieved in the same template or any layout with the get tag.
@set(title: "Admin")
@set(style: "span6")
You can use any expression in set tag
@set(title:"Profile of " + user.login)
Unlike “set”#http://www.playframework.org/documentation/1.2.4/tags#set tag in Groovy template engine, Rythm set tag does not accept tag body. You can use section tag to achieve the same result.
tag
Define an inline tag used in the same template, parent template or child template.
@tag sayHi(String who) {
Hi @who
}
To use the tag defined just follow the common way:
@sayHi("Play!framework")
or pass argument by name:
@sayHi(who: "Play!framework")
verbatim
Define a block of code that not to be parsed by Rythm.
@verbatim() {
OK, anything inside this block is not parsed: @someTag, @someExpression, etc
}
PlayRythm Configuration
PlayRythm provides all default configuration settings that cater for most common use cases.
| configuration | description | default value |
|---|---|---|
rythm.mode | set rythm template engine to run in dev or prod mode | the value of play.mode |
rythm.compactOutput | remove redundant space and line breaks | true in prod mode and false in dev mode |
rythm.enableJavaExtensions | enable Java extension parsing | true |
rythm.default.engine | Indicate which template should be used as default | system (i.e. groovy) |