Writing your own tags

In order to keep repetitive tasks in your template short, you can easily define your own tags. As all you need to know is HTML and the built-in templating language, even pure web developers without backend knowledge can do this.

Getting ready

In this example, we will write a small tag called #{loginStatus /}, which will print the username or write a small note, that the user is not logged in. This is a standard snippet, which you might include in all of your pages, but do not want to write over again.

How to do it...

The following logic is assumed in the controller, here in Application.java:

public static void login(String login, String password) {
   User user = User.find("byLoginAndPassword", login, password).first();
   notFoundIfNull(user);
   session.put("login", user.login);
}

A new tag needs to be created in app/views/tags/loginStatus.html:

<div class="loginStatus">
#{if session.login}
Logged in as ${session.login}
#{/if}
#{else}
Not logged in
#{/else}
</div>

Using it in your own templates is now easy, just put the following in your templates:

#{loginStatus /}

How it works...

The controller introduces the concept of state in the web application by putting something in the session. The parameters of the login method have been (if not specified in routes file) constructed from the request parameters. In this case, from a request, which has most likely been a form submit. Upon calling the controller, the user is looked up in the database and the user's login name is stored in the session, which in turn is stored on the client side in an encrypted cookie.

Every HTML file in the app/views/tags directory is automatically used as a tag, which makes creating tags pretty simple. The tag itself is quite self explanatory, as it just checks whether the login property is set inside the session.

As a last word about sessions, please be aware that the session referenced in the code is actually not a HttpSession as in almost all other Java based frameworks. It is not an object stored on the server side, but rather its contents are stored as an encrypted cookie on the client side. This means you cannot store an arbitrary amount of data in it.

There's more...

You should use tags whenever possible instead of repeating template code. If you need more performance you can even write them in Java instead of using the templating language.

Using parameters and more inside tags

The preceding discussion was the absolute basic usage of tag. It can get somewhat more complex by using parameters or the same sort of inheritance, which is also possible with templates.

Check out http://www.playframework.org/documentation/1.2/templates#tags for more about this topic.

Higher rendering performance by using FastTags

If you need more performance, there is a mechanism called FastTags inside of Play. These tags are actually compiled, as they are written in pure Java. This speeds up execution time. Most of the native supported tags are FastTags in order to keep performance high. For example, take a look at your Play installation inside samples-and-tests/just-test-cases/app/utils/MyTags.java and you will see that these tags are also very simple to implement.

See also

Keep on reading the next recipe, where we will reformat a Date type from boring numbers to a string without using a tag, but a so-called extension.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.176.228