Play Framework template engine

2010-11-16  来源:本站原创  分类:Java  人气:137 

Play frame has its own template engine to generate HTML page, the engine uses Groovy as an expression language. You can use the Groovy language to create dynamic web pages, but all the knowledge without having to learn Groovy, you need to know only a part very similar with Java. Play all the template files are placed in app / views directory, all pages are resolved immediately upon request.

Next we create a simple application:

[email protected]: ~ / dev / play $ / usr / share / play / play new views
~ _ _
~ _ __ | | __ _ _ _ | |
~ | '_ \ | | / _' | | | | _ |
~ | __/|_| \ ____| \ __ (_)
~ | _ | | __ /
~
~ Play! 1.0.3, http://www.playframework.org
~
~ The new application will be created in / home / wichtounet / dev / play / views
~ What is the application name? Views
~
~ OK, the application is created.
~ Start it with: play run views
~ Have fun!
~

Then check the generated files into the app / views directory, we can see the following contents:

* Application: Application of the main controller program stored template
* Errors: storage error page templates, such as 404,500, etc.
* Main.html: Home Page Template

Open Application / index.html, the code is as follows:

# {Extends 'main.html' /}
# {Set title: 'Home' /}

# {Welcome /}

The first line indicates that this template extends main.html, then use the Play command to set the framework to set the page title, these instructions must be closed, the last line of the print line of greeting.

Then we look at main.html template:

<! DOCTYPE html>

<html>
<head>
<title> # {get 'title' /} </ title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<link rel="stylesheet" type="text/css" media="screen" href="@{'/public/stylesheets/main.css'}">
# {Get 'moreStyles' /}
<link rel="shortcut icon" type="image/png" href="@{'/public/images/favicon.png'}">
<script src="@{'/public/javascripts/jquery-1.4.2.min.js'}" type="text/javascript" charset="utf-8"> </ script>
# {Get 'moreScripts' /}
</ Head>
<body>
# {DoLayout /}
</ Body>
</ Html>

This template contains special instructions:

* # {Get 'title' /}: Get the value of the variable title, the value is valid only in the template page
* @ {'/ Public / stylesheets / main.css'}: the introduction of a static resource
* # {DoLayout /}: Insert the contents of sub-template, in this case is the aforementioned index.html page, index.html extends main.html

How to pass parameters between the template it?

Passing parameters is important, for example, we read some data in the controller and the data passed to the view in the show. Play frame in the render method can be used to deal with, for example:

package controllers;

import play.mvc .*;

public class Application extends Controller {
public static void index () {
String hello = "Hello World from Controller!";

render (hello);
}
}

index method to pass a template variable called hello to in the template for the value of this variable, only $ {hello} to:

# {Extends 'main.html' /}
# {Set title: 'Home' /}

Hello from the view
<br />
$ {Hello}

How very simple, right?

Points to a more complex class bar

package models;

public class Book {
private final String title;

public Book (String title) {
super ();

this.title = title;
}

public String getTitle () {
return title;
}
}

And then pass an instance of this controller:

public static void index () {
Book book = new Book ("Hello Play!");
render (book);
}

Then get the object in the template

# {Extends 'main.html' /}
# {Set title: 'Home' /}

Hello from the view
<br />
I've a book for you "$ {book.title}".

This method uses the JavaBean's getting, so we must have getTitle Bean method.

All the dynamic content output, Play frameworks do transcoding processing to prevent cross site XSS attacks, if you do not want to do so, use the raw () methods, such as

$ {Book.title.raw ()}

But this is not a good habit, you only confirm what are the consequences when used.

Note the template as follows:

* {Will not be evaluated by the template engine} *

Arrays and lists

In actual use, regular use of lists and arrays are to, the following is a list of examples of passing:

public static void index () {
List <Book> books = new ArrayList <Book> (3);
books.add (new Book ("Hello Play !"));
books.add (new Book ("Hello Template !"));
books.add (new Book ("Hello Engine !"));
render (books);
}

Template object code using the list as follows:

# {Extends 'main.html' /}
# {Set title: 'Home' /}
I've some books for your:
<ul>
# {List items: books, as: 'book'}
<li> $ {book.title} </ li>
# {/ List}
</ Ul>

It is not very complicated:)

Using a script

If you need to do more complex operations, we can use Groovy scripts can define variables in the script and direct use of other variables, such as:

# {Extends 'main.html' /}
# {Set title: 'Home' /}
I've some books for your:
<ul>
# {List items: books, as: 'book'}
% {
bookTitle = book.title.toUpperCase ();
}%
<li> $ {bookTitle} </ li>
# {/ List}
</ Ul>

You can do, including iteration, conditions a lot of complicated things, but remember, not too complicated to do in the template function, these business logic on the controller or the models, the template should be as simple as possible.

Custom Label

Play frame comes with a lot of tags, but you can create your own number, in order to create a label, we have created in the views directory a subdirectory named tags, for example, we create a booklist.html file, stored in views / tags directory booklist.html code is as follows:

<ul>
# {List items: _items, as: 'book'}
% {
bookTitle = book.title.toUpperCase ();
}%
<li> $ {bookTitle} </ li>
# {/ List}
</ Ul>

Use '_' to get the parameters, in this case _items

With this custom tag, we can modify the above that the template:

# {Extends 'main.html' /}
# {Set title: 'Home' /}
I've some books for your:
# {Booklist items: books /}

Parameters to make the best use of more flexible tag.

Play this we have introduced some basic elements of the template, the template framework more about Play See the official documentation.

相关文章
  • Play Framework template engine 2010-11-16

    Play frame has its own template engine to generate HTML page, the engine uses Groovy as an expression language. You can use the Groovy language to create dynamic web pages, but all the knowledge without having to learn Groovy, you need to know only a

  • Java template engine, Velocity (Reprinted) 2010-03-29

    Many people had seen or learn more about Velocity, the name literally translated as: speed, speed, fast, used in Web development, the used perhaps not many people, most of the basic know and the use of Struts, in the end is how to contact Velocity, a

  • Java template engine - Velocity Applications (original) 2010-03-29

    For the b / s architecture projects, the presentation layer technology, most of the choice of rendering pages jsp, but for a large amount of sites in terms of concurrency, jsp is not a good solution for Java web developers who understand will know ,

  • Javascript front-end application development in the template engine - more with less! 2010-08-17

    We use JavaScript for front-end development time and do most things, apart from dealing with dom other than that around the json data to operate. The data manipulation is the most trouble with json generated dom object, and usually write a bunch for,

  • Introduction Smarty template engine 2010-12-07

    PHP, MVC development model to achieve the logic layer and presentation layer to choose from a variety of template engine, but after the birth of the official engine SMARTY choose there will be a change. It is the concept and implementation are very "

  • Smarty php template engine 2010-04-26

    from: http://www.smarty.net/manual/en/language.function.if.php (If), (elseif), (else) (IF) statements in Smarty have much the same flexibility as PHP IF statements, with a few added features for the Template Engine. Every (IF) must be paired with a m

  • python template engine in the field of transfer 2010-06-10

    GenShi Features: interpreted template, simply for the xml, streaming processing mechanism, can be embedded python statements and expressions, providing a powerful feature. Evolved from the Kid, more flexible than the Kid, performance is also better t

  • Velocity Java template engine user manual (1) 2010-08-26

    Velocity Java template engine user manual (1) Posted: 2006.04.17 08:50 Source: Saidi Bo off of: itzealot 1, Velocity is what? Velocity is a java-based template engine (template engine). It allows anyone to use just a simple template language (templat

  • Heyes Template Class php template engine 2010-09-11

    Program Name: Heyes Template Class php template engine developer / Organization Name: Richard Heyes Operating environment: php Official Website: http://www.phpguru.org Download: http://www.phpguru.org/downloads / template / Source: http://www.osphp.c

  • Asp.net template engine technology 2010-10-20

    Had heard my friends talking about php template engine technology which, when half-comprehended when I feel really strong, have been thinking asp.net have this technology? I do not know if my understanding is not right. Actually asp.net template engi

  • phpnew template engine class 2010-11-01

    <?php /* # @category: Share Studio ; # @copyright: Share Online Media Corporation ; # @var; PHPnew.class ; # @version: PHPnew 7.0 Professional; */ // Template engine parse class ; class PHPnew { const PHP = 5; //PHP Limited version ; public $template

  • 30 minutes speed-pass, PHP Smarty template engine 2010-11-09

    30 minutes speed-pass, PHP Smarty template engine Category: Technical Documentation PHP + MySql Tags: Yes Yes PHP100 following the PHP tutorials about the concentration of the video 27,28,29,30, and add some smarty Chinese manual supplementary materi

  • Acquaintance of the smarty template engine PHP 2010-11-26

    php embedded HTML scripting language, HTML, and php code in order to separate the so-called logic layer and presentation layer, which is the purpose template engine. To achieve this purpose template engine must have the function are: 1. Store variabl

  • Introduce visitors to learn Smarty template engine - PHP 2010-12-21

    PHP, MVC development model to achieve the logic layer and presentation layer to choose from a variety of template engine, but after the birth of the official engine SMARTY choose there will be a change. Its concept and implementation is very "avant g

  • php in smarty template engine 2010-12-29

    php embedded HTML scripting language, HTML, and php code in order to separate the so-called logic layer and presentation layer, which is the purpose template engine. To achieve this purpose template engine must have the function are: 1. Store variabl

  • Arch-05-12-JQuery use template engine tmpl $ {value} and freemarker symbol conflict 2011-07-04

    Arch-05-12-JQuery use template engine tmpl $ {value} and freemarker symbol of conflict, the solution: The original code, freemarker compilation error <td>${name}</td> Substitution method, Cadogan {"$"}: <td>${"$"}{nam

  • 如何选择Javascript模板引擎(javascript template engine)? 2012-09-17

    日期:2012-9-17 来源:GBin1.com 随着前端开发的密集度越来越高,Ajax和JSON的使用越来越频繁,大家肯定免不了在前台开发中大量的使用标签,常见到的例子如下: 你的到了一个JSON对象,如下: var data={ email: '[email protected], gender: 'male' } 然后你需要将json数据组织成页面内容,如下: var email, gender; email= '<div class="mail">' + dat

  • QUI continue the - add JS template engine function, assembly LOGO 2009-08-15

    <?xml version="1.0" encoding="UTF-8"?> <data> <widget> <![CDATA[ <table cellspacing="0" cellpadding="0" width="286" height="88" border="0"> <tr> <td

  • HTML generated using the Velocity template engine 2010-12-08

    Dynamic content generated HTML. The benefits we should have little understanding of the Internet more. Now I will explain to you how to use Velocity generate HTML. On the Chinese, I have been introduced in another where, how to set the Velocity of th

  • handlebars: a power javascript template engine 2013-04-18

    Handlebars provides the power necessary to let you build semantic templates effectively with no frustration. Mustache templates are compatible with Handlebars, so you can take a Mustache template, import it into Handlebars, and start taking advantage