Community contributed extensions

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) 

_au

Absolute reverse lookup URL.

@_au(Applicaiton.index)

The above code is equal to the following groovy template code:

@@{Application.index}

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.

<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:

<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.modeset rythm template engine to run in dev or prod modethe value of play.mode
rythm.compactOutputremove redundant space and line breakstrue in prod mode and false in dev mode
rythm.enableJavaExtensionsenable Java extension parsingtrue
rythm.default.engineIndicate which template should be used as defaultsystem (i.e. groovy)

See Also

  1. Integrate PlayRythm into your Play project
  2. Rythm Template User guide