Wiki source code of Write a Template

Last modified by Eleni Cojocariu on 2026/12/04 14:31

Show last authors
1 [[Templates>>doc:documentation.xs.dev.look-feel.templates.WebHome]] explains where XWiki looks for the file and with which rights it runs it.
2
3 1. Create the file under the folder that matches the kind of template you need:(((
4 {{code language="none"}}
5 src/main/resources/templates/mytemplate.vm a classloader template, packaged in the JAR
6 src/main/webapp/templates/mytemplate.vm a filesystem template, packaged in the WAR
7 {{/code}}
8
9 The file name is the template name, so a template added by an extension needs a name specific enough not to collide with a template of the platform.
10 )))
11 1. Write the content as a Velocity script producing HTML:(((
12 {{code language="velocity"}}
13 #set ($message = $services.localization.render('myextension.greeting'))
14 <div class="box infomessage">$escapetool.xml($message)</div>
15 {{/code}}
16 )))
17 1. Declare the properties the template needs on the leading lines, before any content:(((
18 {{code language="none"}}
19 ##!source.syntax=xwiki/2.1
20 {{info}}
21 $escapetool.xml($message)
22 {{/info}}
23 {{/code}}
24
25 Those lines are removed before the content is executed, so Velocity never sees them. Here ##source.syntax## makes the result be parsed as XWiki syntax instead of being injected as HTML, which is what lets the template use a macro. [[Template Properties>>doc:documentation.xs.dev.look-feel.templates.template-properties.WebHome]] lists the keys.
26 )))
27 1. Include the template from a wiki page to run it:(((
28 {{code language="none"}}
29 {{template name="mytemplate.vm"/}}
30 {{/code}}
31 )))
32 1. Check the result: the macro is replaced by what the template produced.(((
33 {{code language="html"}}
34 <div class="box infomessage">Hello</div>
35 {{/code}}
36 )))

Get Connected