Template Module
Provide APIs to manipulate templates |
Type | JAR |
Category | API |
Developed by | |
Rating | |
License | GNU Lesser General Public License 2.1 |
Bundled With | XWiki Standard |
Compatibility | Since 7.0M1 |
Table of contents
Description
This module provide APIs and tool to execute XWiki templates. What we call templates are generally files or wiki pages responsible for the display of the general UI (everything around the actual document content execution).
Use a template
Template manager
The main entry point of the module is org.xwiki.template.TemplateManager component.
A template is represented by org.xwiki.template.Template interface which provide access to the template content (as a org.xwiki.template.TemplateContent) as well as its path and id.
- find the template with id mytemplate.vm:@Inject
TemplateManager templateManager;
...
Template template = templateManager.getTemplate("mytemplate.vm"); - find and execute the template with id mytemplate.vm:@Inject
TemplateManager templateManager;
...
String html = templateManager.render("mytemplate.vm"); - find and execute the template with id mytemplate.vm and get a XDOM:@Inject
TemplateManager templateManager;
...
XDOM result = templateManager.execute("mytemplate.vm"); - find and parse the template with id mytemplate.vm without executing it:@Inject
TemplateManager templateManager;
...
XDOM xdom = templateManager.getXDOM("mytemplate.vm");
Template macro
A wiki macro is provided to make easier to include template in wiki pages. See Template Macro for more details.
Execute and include the result of template mytemplate.vm template:
Scripting
A template Script Service to make easier execute a template from a script.
Execute and render template:
{{html}}
$services.template.render('mytemplate.vm')
{{/html}}
{{/velocity}}
Write a template
Location
Template manager is looking for template in the following order:
- A resource name in the current skin (or one of its parent skin)
- A relative file path in "templates/" folder of the WAR
- templates/" folder. A relative file path in the classloader (usually inside a JAR file) in "
Content
By default template are Velocity scripts usually producing html content.
But the content also support a set of properties that can modify the behavior and how it's parsed among other things. These properties are located at the beginning of the content and use the syntax ##!property=value as in:
{{info}}
Some info message
{{/info}}
Here is the list of supported properties:
Property name | Values | Default | Description |
---|---|---|---|
source.syntax | A Rendering Input Syntax | The identifier of the parser to use to interpret the content of the template | |
raw.syntax | A Rendering Input Syntax | xhtml/1.0 | When source.syntax is not set, it's the syntax that will be put in the RawBlock produced with the result of the Velocity execution |
author | The reference of a XWiki user | The user to use as author when executing the template. Only taken into account for filesystem and classloader templates. Wiki templates are always executed with the right of their author so this property is not taken into account. | |
privileged | true/false | true since 9.0RC1, false before | Force the filesystem or classloader templates to have Programming Rights. In practice it means its author will be xwiki:XWiki.superadmin. Wiki templates are always executed with the right of their author so this property is not taken into account. |
cacheAllowed | true/false | false | Indicate if caching the result is allowed |
asyncAllowed | true/false | false | Indicate if executing the template asynchronously is allowed |
contextEntries | context entries | The list of entries from the context the template needs | |
unique | REQUEST | Make sure the template is executed only once in the same request |
Wiki templates
See How to override a template.
Filesystem templates
That's the templates located on the filesystem usually in two locations:
- /templates/ folder in the WAR
- in filesystem skins (usually /skins/<skin>/)
Since 9.0 they have programming right by default. Before that they were executed with the right of the current document content author.
Classloader templates
Those are the templates found in the classloader in templates package (usually in JARS or in /WEB-INF/classes/templates in the WAR).