Templating
The templating module is used throughout this and most of the other applications which ship with eXist. Its design has one goal: a clean separation of concerns. All views are plain, valid HTML5. They do not include any XQuery or other executable code. Application code should go into separate XQuery modules and will be called automagically by the templating framework.
This document provides a number of simple, working examples. For a detailed description of the features of the templating framework, refer to the documentation.
The templating module scans the HTML for elements with class attributes following a simple convention and tries to translate them into XQuery function calls. In the simplest case, a class attribute which triggers a function call just contains the name of a function in an XQuery library known to the system. For example:
The expanded output of this template call is shown below:
Here's the code for the templating function. The two parameters are required for any function to be used by the templating framework:
- $node
- The HTML node being processed. This is the node with the class attribute which triggered the templating call.
- $model
- Application data which may have been provided by template functions further up in the document (see below)
Note: instead of putting template calls into class attributes, you can also use the HTML5-compliant method: specify the template call as well as any optional parameters (see the section below) in data attributes:
Parameter Injection
Very often, you will also need to pass static parameters to the template. This is done by appending a query string to the template call:
This calls the following function:
Parameters can be static or dynamic. Static parameters are specified in the HTML as in the example above. Dynamic parameters are read from the HTTP request or HTTP session. The templating framework automatically tries to determine a value for a given function parameter by looking at those alternatives in turn. If you add "?n1=2&n2=4" to the location URL of this page in your browser, you'll see how the output below will change:
Again, the expanded output is shown below:
Annotations
By default, the return value of a templating function will replace the HTML node it was called for. This means the
element will be lost unless you copy it. To avoid manually copying the wrapper element, the %templates:wrap
annotation does just that.
There's also an annotation %templates:default
to define a fallback value for a parameter.
This function could be called with:
Please note the extra class "hi", which should color the div. Without %templates:wrap, it would have been lost. Output below:
Nested Template Calls
Templating calls can be nested, which enables us to build more complex HTML structures. For example:
ex:addresses
retrieves a set of addresses from the database and puts them into the $model
.
templates:each
iterates through the model items and processes its inner HTML once for each item.
Finally, the ex:print-name
and friends print out a specific field of the address.
The corresponding XQuery functions are:
Again, the HTML output is displayed below:
Lothar Lärche | Vogelstraße 67 | Mainz |
Elsa Elster | Vogelstraße 22 | Mainz |
Berta Muh | An der Viehtränke 13 | Wiesbaden |
Hans Hase | Feldstraße 44 | Wiesbaden |
Biene Maja | Wiesenweg 33 | Berlin |
Linda Klaus | Kopfstraße 5 | Heidelberg |
Rudi Rüssel | An der Viehtränke 24 | Frankfurt |
Use the Templating from other Contexts
The HTML templating module can also be used outside eXist's default app setup. For example, one may want to call the templating from within a RestXQ module. Because RestXQ uses a separate module for access to HTTP request parameters, you have to supply one additional lookup function for resolving parameters when initializing the templating. The code below demonstrates this:
Source Links
- The template functions used for the examples in this page.
- The main XQuery which triggers the templating in this app.
- Shakespeare app: the HTML and the XQuery module implementing the templating functions.