concept template in category scala

appears as: templates, A template, template, templates, template
Play for Scala: Covers Play 2

This is an excerpt from Manning's book Play for Scala: Covers Play 2.

This is a Scala template. The first line defines the parameter list—a name parameter in this case—and the HTML document includes an HTML em tag whose content is a Scala expression—the value of the name parameter. A template is a Scala function definition that Play will convert to normal Scala code and compile. Section 3.5.4 explains how templates become Scala functions in more detail.

To use this template, you have to render it in the hello action method to produce its HTML output. Once Play has converted the template to a Scala object called views .html.hello, this means calling its apply method. You then use the rendered template as a String value to return an Ok result:

Play Scala templates are HTML files containing snippets of Scala code, which are compiled into plain Scala code before your application is started. Play templates are type-safe, which isn’t common among web frameworks.

In most frameworks, templates aren’t type-safe and are evaluated at runtime, which means that problems in a template only show up when that particular template is rendered. These frameworks don’t help you detect errors early, and this causes fragility in your application. In this section, we’ll compare a regular template engine that’s not type-safe with Play’s type-safe template engine.

As an example, we’ll build a catalog application. The main page will be a list of all the articles in the catalog, and every article on this page will have a hyperlink to a details page for that article, where more information about that article is shown. We’ll first show how this is done with the Play 1.x template engine and then compare it with the type-safe template engine in Play 2. This will allow us to illustrate some of the weaknesses of a template engine that’s not type-safe, which will let you appreciate the type safety of Play 2’s templates even more.

Figure 6.6. Escaping in templates
Lift in Action: The Simply Functional Web Framework for Scala

This is an excerpt from Manning's book Lift in Action: The Simply Functional Web Framework for Scala.

Template views are the most commonly used method of generating view content, and they require that you have a well-formed XHTML or HTML5 template. It’s important to note that Lift doesn’t allow you to use view code that’s invalid; this means that when you’re working with a design team, if their templates are W3C-validate, you know they’ll work with Lift because the snippet invocations are also part of the markup. This ensures that designers don’t inadvertently introduce problems by altering framework-specific code within the template, which is a common problem with other frameworks.

Fortunately both the Java platform and Lift provide some great features to help you make the best of this rather complex situation. The first section of this chapter looks at how you can implement Lift’s localization helpers in your templates and application code. Then, the second section covers how you actually obtain localized content—where your code or template defines one of Lift’s localization helpers, and where the content that ultimately replaces it comes from. There are three possible options and they’re all illustrated with examples in section 12.2.

When Lift receives a request and then goes looking for the template, it will also include a set of locale-specific templates in that search as well. Suppose you have index.html as your content file, but you want to make a special case for German. In order to implement this, you’d have both an index.html and an index_de.html file (or one that also specifies the country: index_de_DE.html).