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 Enterprise |
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
- 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.
It's possible to control various behavior of those template with the following properties:
Property name | Values | 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 | 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 use to use as author or the template content when executing it, only taken into account for file system templates, wiki based template will be given their actual author. |
privileged | true/false | Force the filesystem template to have Programming Rights. In practice it means its author will be xwiki:XWiki.superadmin |
Wiki templates
See How to override a template.