View tags allow developers to create reusable view functions and components and make the management of views a lot simpler and easier.
For Java, we need to take the following steps:
Run the
foo_java
application with Hot-Reloading enabled.Create the tag file
productsIterator.scala.html
infoo_java/app/views/tags
Add the contents of the tag file:
@(products: Collection[String]) <ul> @for(product <- products) { <li>@product</li> } </ul>
Edit
foo_java/app/views/products.scala.html
by adding the following block:@import tags._ @productsIterator(products)
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:
Run the
foo_scala
application with Hot-Reloading enabled.Create the tag file
productsIterator.scala.html
infoo_scala/app/views/tags
Add contents of the tag file:
@(products: Seq[String]) <ul> @for(product <- products) { <li>@product</li> } </ul>
Edit
foo_scala/app/views/products.scala.html
by adding the following block:@import tags._ @productsIterator(products)
Reload the products page using a web browser to see the new products listing, using an unordered list HTML tag:
http://localhost:9000/products
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)
).