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.
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
1 2 3 4 5 6 7 8 9 10
<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
1 2 3
<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.
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
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.
We have conducted a high-level exploration of Lift’s templating. We discussed template surrounds and bind points, embedded Templates, and head merge.