Book Image

Play Framework Cookbook - Second Edition

By : Alexander Reelsen, Giancarlo Inductivo
Book Image

Play Framework Cookbook - Second Edition

By: Alexander Reelsen, Giancarlo Inductivo

Overview of this book

<p>As web and mobile systems become more sophisticated, anchoring systems in a mature, solid framework has become increasingly important. Play 2 provides developers with the necessary tools to build robust web applications.</p> <p>This book is a compilation of useful recipes aimed at helping developers discover the power of Play 2. The introductory section serves as a primer to Play Framework, wherein all the fundamentals of the framework are covered extensively. It then explains the usage of controllers and how modules can be leveraged for optimal performance. Next, the book walks you through creating and using APIs, followed by extensive real-world applications. Finally, you will learn to manage applications post production.</p>
Table of Contents (15 chapters)
Play Framework Cookbook Second Edition
Credits
About the Authors
About the Reviewers
www.PacktPub.com
Preface
Index

Using helper tags


View tags allow developers to create reusable view functions and components and make the management of views a lot simpler and easier.

How to do it...

For Java, we need to take the following steps:

  1. Run the foo_java application with Hot-Reloading enabled.

  2. Create the tag file productsIterator.scala.html in foo_java/app/views/tags

  3. Add the contents of the tag file:

        @(products: Collection[String])
    
        <ul>
          @for(product <- products) {
            <li>@product</li>
          }
        </ul>
  4. Edit foo_java/app/views/products.scala.html by adding the following block:

        @import tags._
     
        @productsIterator(products)
  5. Reload the products page using a web browser to see the new product listing, using an unordered list HTML tag:

    http://localhost:9000/products
    

For Scala, we need to take the following steps:

  1. Run the foo_scala application with Hot-Reloading enabled.

  2. Create the tag file productsIterator.scala.html in foo_scala/app/views/tags

  3. Add contents of the tag file:

        @(products: Seq[String])
    
        <ul>
          @for(product <- products) {
            <li>@product</li>
      }
        </ul>
  4. Edit foo_scala/app/views/products.scala.html by adding the following block:

        @import tags._
     
        @productsIterator(products)
  5. Reload the products page using a web browser to see the new products listing, using an unordered list HTML tag:

    http://localhost:9000/products
    

How it works...

In this recipe, we were able to create a new view tag in app/views/tags. We proceeded to use this tag in our View template.

First, we created a new tag that receives a collection of product titles, from which it is then displayed in the template as an unordered list. We then imported the tag in our products View template and invoked the helper function by calling it using its filename (@productsIterator(products)).