Templates in Lift Framework

SHARE & COMMENT :

This article is based on Lift in Action, to be published on July 2011. It is being reproduced here by permission from Manning Publications. Manning publishes MEAP (Manning Early Access Program,) ebooks and pbooks. MEAPs are sold exclusively through Manning.com. All print book purchases include an ebook free of charge. When mobile formats become available all customers will be contacted and upgraded. Visit Manning.com for more information.

also read:

Creating Templates

Introduction

Templates in Lift are always fully valid, XHTML markup—Lift does not let you write invalid markup. In an abstract sense, think of these Templates as XML input to your snippets that actually do the dynamic transformation. However, Templates have a lot more functionality than just being a place to invoke snippets! In the same way that Lift helps keep your server code cleanly separated, Lift offers some convenient helpers for your Templates as well.

Surrounds and bind points

More often than not, your application will use a single template or a small collection of top-level Templates that contain the majority of the markup. Each “page” has a much smaller template that contains the calls to those snippets that are needed to provide the various dynamic items for the said page. In Lift, we wrap these smaller page fragments with what we refer to as a surround (see listing 1).

Listing 1 Example of template surround

<html xmlns="http://www.w3.org/1999/xhtml" xmlns:lift="http://liftweb.net/">
		<head>
			<title>demo:demo:1.0-SNAPSHOT</title>
		</head>
		<body>
			<lift:bind name="content" /> 			#1
		</body>
</html>
#1 A template binding point referenced by the name content

Listing 1 is an example of a template that could have page content inserted at the bind point called content. It is important to note that you can have as many bind points as you like, and not all the points have to be used within a given page rendering. On the other hand, the page level template can specify the surrounding template with which it will be wrapped. An example of defining a page-level template surround can be seen in listing 2.

Listing 2 Example of page level surround

<lift:surround with="default" at="content">
		<h2>Your content goes here</h2>
	</lift:surround>

Surrounds take two parameters. The first is with, which defines the template with which this sequence of nodes will be wrapped. Templates should be located within the templates-hidden directory. The second parameter is at, which defines at what bind point in the parent template this page content will be inserted.

Embedded templates

While building your application, you may need to insert the XHTML from another template to keep it common. For instance, a form for adding a product to a system would likely have the same form as the one needed for editing that product in a different section of the system. Lift has this covered: listing 3 shows an example of using template embedding.

Listing 3 Embedding Templates

<lift:embed what="/templating/_sample_embed"/>

By allowing you to arbitrarily embed Templates into one another, you don’t have to worry about duplicating your presentation code—this can be an extremely effective technique when your system grows beyond the very trivial.

Head merge

Another common idiom that most applications require is to have page-specific items such as JavaScript and stylesheet elements in the <head> of a page. Lift has some nifty tooling for this. All you need to do is define the <head> element inside of a surround element and Lift will automatically merge that content with the top <head> element.

Summary

We have conducted a high-level exploration of Lift’s templating. We discussed template surrounds and bind points, embedded Templates, and head merge.

Comments

comments

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Speak Your Mind

*

Close
Please support the site
By clicking any of these buttons you help our site to get better