Play Framework Template Engine

Play framework has a proficient template framework which permits to alertly create HTML, XML, JSON or any content based organization report. The template engine utilizes Groovy as a statement dialect. A tag framework permits you to make reusable capacities. Templates are put away in the application/sees directory.

Template Syntax

A template document is a content record, a few parts of which have placeholders for alertly created substance. Dynamic components of the template are composed utilizing the Groovy dialect. Cool’s grammar is near Java’s. Dynamic components are determined amid template execution. The rendered result is then sent as a component of the HTTP reaction.

Statements: ${…}

The least complex approach to make an element component is to pronounce a statement. The linguistic use utilized here is ${… }. The consequence of assessing the statements is embedded set up of the articulation. For instance:

<h1>client ${client.name}>/h1>
<strong> </strong>

Tags: # {tagname/}

A tag is a template part that could be called with parameters. On the off chance that the tag has stand out parameter, by gathering it is called “arg” and its name could be excluded. Case in point, this tag embeds a SCRIPT tag to load a Javascript record:

#{script "jquery.js"/}

A tag must be shut, either specifically or by an end tag:

#{script "jquery.js"/}

Or

#{script 'jquery.js'}#{/script}

Case in point the rundown tag permits emphasis over any gathering. It takes two obligatory parameters:

<h1>client ${client.name}</h1><ul>#{list items:client.accounts, as:'account' }
<li>${account}</li>
#{/list}
</ul>

Actions : @{… } or @@{… }

You can utilize the Router to (turn around) produce a URL relating to a detailed course. From a template you can utilize the unique @{… } punctuation to do that. Case in point:

<h1>client ${client.name}</h1><p><a href="@{clients.showaccounts(client.id)}">all accounts</a>
</p>
<hr/>
<a href="@{clients.index()}">back</a>

The @@{… } linguistic use does likewise however creates an outright URL (remarkably helpful for email, …).

Messages: & {…}

In the event that your application needs internationalization you can utilize the &{… } grammar to show an internationalized message:

Case in point in the records conf/messages we determine:

clientname=the client name is %s

To show this message in a template, basically utilization:

<h1>&{'clientname', client.name}</h1>

Comment: *{…}*

Comments aren’t assessed by the template engine. They are simply comments…

*{**** Display the client name ****}*

<div>${user.name}</div> 

<div>${user.name}</div>

Scripts: %{…} %

A script is a more confounded situated of statements. A script can pronounce a few variables and characterize a few articulations. Utilize the {… } punctuation to embed a script.

%{fullname = client.name.touppercase()+' '+client.forname;}%
<h1>client ${fullname}</h1>

A script can compose element content straightforwardly utilizing the out article:

%{fullname = client.name.touppercase()+' '+client.forname;out.print('<h1>'+fullname+'</h1>');
}%

You can utilize a script to make a structure, for example, a cycle in your template:

<h1>client ${client.name}</h1><ul>%{
for(account in client.accounts) {
}%
<li>${account}</li>
%{
}
}%
</ul>

Take notice that a template is not a spot to do complex things. In this way, utilize a tag when you can, or move the processing into the controller.

Make tags

You can undoubtedly make specifics tags for your application. A tag is a straightforward template document, put away in the application/perspectives/tags directory. The name of the template record is utilized as the tag name. To make a welcome tag, simply make the application/perspectives/tags/hello.html record.

Hi from tag!

No compelling reason to design anything. You can utilize the tag specifically:

#{hello/}
Recover tags parameters

Tag parameters are uncovered as template variables. The variable names are developed with the “_” character prepended to the parameter name

Hi ${_name} !<span style="line-height: 19px;"> </span>

What’s more you can pass the name parameter to the tag:

#{hello name:'bob'/}

On the off chance that your tag has one and only parameter, you can utilize the actuality than the default parameter name is arg and that its name is verifiable.

Illustration:

Hi ${_arg}!

What’s more you can call it effectively utilizing:

#{hello "Weave"/}

On the off chance that your tag upholds a body, you can incorporate it sometime or another in the tag code, utilizing thedobody tag. For instance:tag body

Hi #{dobody/}!

 

Furthermore you can then pass the name as tag body:

#{hello}Rits#{/hello}