8.4.14 include, import
include and import allow to include other template files into the current one, hence supporting modularisation and re-use.
An include/import will cause the included template to be executed at the moment the include/import instruction is encountered. Any output produced by the included/imported template will be inserted at that location.
The difference between include and import is that and import will never override macro definitions from the template it is included in. An include on the other hand behaves as if the content of the included template would be inserted at the location of the include instructions: any macro will override previously defined macro's with the same name.
Syntax
<t:include src="..." mode="..."/> <t:import src="..." mode="..."/>
Attributes:
- src: URI of the template to include
- this attribute is evaluated at runtime, hence can contain expressions.
- relative URIs will be resolved against the URI from which the current template was loaded.
- recursive inclusions are detected and disallowed.
- mode: (optional) either "silent" or "regular" (default). When mode is silent, the template will be executed, but no output will be produced. This is useful for templates that only contain macro's or variables. It will avoid that the root element and the whitespace between the macro's will be outputted.
Usage
Includes and imports can occur at any location in a template file. They are not only useful for including macro libraries, but also for reusable snippets.
The same template can be included multiple times.
Supress root element trick
If you have a "snippet" template to be included in other templates, but you don't want the template to produce a root element, you can use a t:if element as root for your template:
<?xml version="1.0"?>
<t:if test="${true}" xmlns:t="http://kauriproject.org/template">
Hello world!
</t:if>
See also
For reusable layouts, be sure to check out the
Previous