Wiki source code of Write a Template

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

Hide last authors
Eleni Cojocariu 7.4 1 [[Templates>>doc:documentation.xs.dev.look-feel.templates.WebHome]] explains where XWiki looks for the file and with which rights it runs it.
Eleni Cojocariu 2.1 2
Eleni Cojocariu 7.4 3 1. Create the file under the folder that matches the kind of template you need:(((
Eleni Cojocariu 2.1 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 )))
Eleni Cojocariu 7.4 11 1. Write the content as a Velocity script producing HTML:(((
Eleni Cojocariu 2.1 12 {{code language="velocity"}}
13 #set ($message = $services.localization.render('myextension.greeting'))
14 <div class="box infomessage">$escapetool.xml($message)</div>
15 {{/code}}
16 )))
Eleni Cojocariu 7.4 17 1. Declare the properties the template needs on the leading lines, before any content:(((
Eleni Cojocariu 2.1 18 {{code language="none"}}
19 ##!source.syntax=xwiki/2.1
20 {{info}}
21 $escapetool.xml($message)
22 {{/info}}
23 {{/code}}
24
Eleni Cojocariu 7.5 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.
Eleni Cojocariu 2.1 26 )))
Eleni Cojocariu 7.4 27 1. Include the template from a wiki page to run it:(((
Eleni Cojocariu 2.1 28 {{code language="none"}}
29 {{template name="mytemplate.vm"/}}
30 {{/code}}
31 )))
Eleni Cojocariu 7.4 32 1. Check the result: the macro is replaced by what the template produced.(((
Eleni Cojocariu 2.1 33 {{code language="html"}}
34 <div class="box infomessage">Hello</div>
35 {{/code}}
36 )))

Get Connected