Wiki source code of Execute a Template
Last modified by Eleni Cojocariu on 2026/12/04 14:32
Show last authors
| author | version | line-number | content |
|---|---|---|---|
| 1 | [[Templates>>doc:documentation.xs.dev.look-feel.templates.WebHome]] explains how the name you pass is resolved. | ||
| 2 | |||
| 3 | 1. Inject the component:((( | ||
| 4 | {{code language="java"}} | ||
| 5 | @Inject | ||
| 6 | private TemplateManager templateManager; | ||
| 7 | {{/code}} | ||
| 8 | ))) | ||
| 9 | 1. Look the template up, when you need to know whether it exists before using it:((( | ||
| 10 | {{code language="java"}} | ||
| 11 | Template template = this.templateManager.getTemplate("mytemplate.vm"); | ||
| 12 | {{/code}} | ||
| 13 | |||
| 14 | The result is ##null## when no source answers the name. ##Template## gives access to the id and the path of the template, and to its content as a ##TemplateContent##. | ||
| 15 | ))) | ||
| 16 | 1. Render the template, which executes it and returns the result in the output syntax:((( | ||
| 17 | {{code language="java"}} | ||
| 18 | String html = this.templateManager.render("mytemplate.vm"); | ||
| 19 | {{/code}} | ||
| 20 | ))) | ||
| 21 | 1. Produce blocks instead, when the result is to be inserted in a page that is being rendered:((( | ||
| 22 | {{code language="java"}} | ||
| 23 | XDOM result = this.templateManager.execute("mytemplate.vm"); | ||
| 24 | Block inline = this.templateManager.execute("mytemplate.vm", true); | ||
| 25 | XDOM parsed = this.templateManager.getXDOM("mytemplate.vm"); | ||
| 26 | {{/code}} | ||
| 27 | |||
| 28 | ##execute## runs the template, and its second form produces blocks fit for an inline context such as the inside of a paragraph. ##getXDOM## parses the template without executing it. | ||
| 29 | ))) | ||
| 30 | 1. Check what the call returned, here for a template producing an information box:((( | ||
| 31 | {{code language="html"}} | ||
| 32 | <div class="box infomessage">Some info message</div> | ||
| 33 | {{/code}} | ||
| 34 | ))) |