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:
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:
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:
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: