Template Module

Version 17.2 by Vincent Massol on 2020/11/13 17:22

cogProvide APIs to manipulate templates
TypeJAR
CategoryAPI
Developed by

XWiki Development Team

Rating
0 Votes
LicenseGNU Lesser General Public License 2.1
Bundled With

XWiki Standard

Compatibility

Since 7.0M1

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:

{{template name="mytemplate.vm"/}}

Scripting

A template Script Service to make easier execute a template from a script.

Execute and render template:

{{velocity}}
{{html}}
$services.template.render('mytemplate.vm')
{{/html}}
{{/velocity}}

Write a template

Location

Template manager is looking for template in the following order:

  1. A resource name in the current skin (or one of its parent skin)
  2. A relative file path in "templates/" folder of the WAR
  3. [since 8.3] A relative file path in the classloader (usually inside a JAR file) in "templates/" folder.

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:

##!source.syntax=xwiki/2.1
{{info}}
Some info message
{{/info}}

Here is the list of supported properties:

Property nameValuesDefaultDescription
source.syntaxA Rendering Input SyntaxThe identifier of the parser to use to interpret the content of the template
raw.syntaxA Rendering Input Syntaxxhtml/1.0When source.syntax is not set, it's the syntax that will be put in the RawBlock produced with the result of the Velocity execution
authorThe reference of a XWiki userThe 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.
privilegedtrue/falsetrue since 9.0RC1, false beforeForce 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.
cacheAllowedtrue/falsefalsesince 11.8 Indicate if caching the result is allowed
asyncAllowedtrue/falsefalsesince 11.8 Indicate if executing the template asynchronously is allowed
contextEntriescontext entriessince 11.8 The list of entries from the context the template needs
uniqueREQUESTsince 11.8 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).

Get Connected