Book Image

Play Framework Cookbook

By : Alexander Reelsen
Book Image

Play Framework Cookbook

By: Alexander Reelsen

Overview of this book

<p>The Play framework is the new kid on the block of Java frameworks. By breaking with existing standards the play framework tries not to abstract away from HTTP as most web frameworks do, but tightly integrates with it. This means quite a shift for Java programmers. Understanding these concepts behind the play framework and its impact on web development with Java are crucial for fast development of applications.<br /><br />The Play Framework Cookbook starts where the beginner documentation ends. It shows you how to utilize advanced features of the Play framework &ndash; piece by piece and completely outlined with working applications!<br /><br />The reader will be taken through all layers of the Play Framework and provided with in-depth knowledge from as many examples and applications as possible. Leveraging the most from the Play framework means to think simple again in a java environment. Implement your own renderers, integrate tightly with HTTP, use existing code, improve site performance with caching and integrate with other web services and interfaces. Learn about non-functional issues like modularity or integration into production and testing environments. In order to provide the best learning experience during reading Play Framework Cookbook, almost every example is provided with source code, so you can start immediately to integrate recipes into your own play applications.</p>
Table of Contents (16 chapters)
Play Framework Cookbook
Credits
Foreword
About the Author
About the Reviewers
www.PacktPub.com
Preface
Further Information About the Play Framework
Index

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.