Book Image

Learning Play! Framework 2

By : Andy Petrella
Book Image

Learning Play! Framework 2

By: Andy Petrella

Overview of this book

<p>The Learning Play! Framework 2 has been created for web developers that are building web applications. The core idea is to focus on the HTTP features and to enable them through a simplification lens. Building a web application no longer requires a configuration phase, an environment setup, or a long development lifecycle - it's integrated!<br /><br />Learning Play! Framework 2 will enable any web developers to create amazing web applications taking advantage of the coolest features. It's the fastest way to dive into Play!, focusing on the capabilities by using them in a sample application. Although essentially Java based code, a Scala version is presented as well – giving an opportunity to see some Scala in action.<br /><br />After setting up the machine and learning some Scala, you will construct an application which builds from static to dynamic, before introducing a database. <br /><br />Then we'll focus on how data can be consumed and rendered in several ways. This will enable some real time communication through WebSocket and Server-Sent Event – on both server and client sides.</p> <p>The book will end with testing and deployment, which completes any web development project.</p>
Table of Contents (20 chapters)
Learning Play! Framework 2
Credits
About the Author
Acknowledgement
About the Reviewers
www.packtpub.com
Preface
Materials
Index

Shape it, compose it, re-use it


This first section will concentrate on what a template is, its structure, and its features. We'll see how it can help us to easily create views in a composable and sharable fashion.

Creating our first template

A template in Play! Framework 2 is basically a file with a specific extension that commonly resides under the views package. So, a template filename always has the following pattern:

<template-name>.scala.<content-type>

It is composed of the following:

  • A template name, which must be formatted like a variable (for example, listContainer). This will be used to reference it in the controllers.

  • A first extension part, .scala, which is always the same. As stated earlier, templates are Scala-based.

  • A second extension part, which is the real type of the data. Out of the box types are .html, .xml, and .txt, but Play! 2 is extensible enough to enable us to add new ones.

Play! 2 will detect these files based on the pattern and will compile them into functions...