Wiki source code of Display an Icon in a Script

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

Show last authors
1 1. Print the icon by its name in [[the XWiki Icon Set>>doc:documentation.xs.admin.look-feel.icons.icon-set.WebHome]], which returns XWiki syntax(((
2 {{code language="velocity"}}
3 The $services.icon.render('home') button returns to the wiki home page.
4 {{/code}}
5 )))
6 1. Call ##renderHTML## instead where the surrounding output is already HTML, which returns the markup itself(((
7 {{code language="velocity"}}
8 {{html}}<button class="btn btn-default">$services.icon.renderHTML('home') Home</button>{{/html}}
9 {{/code}}
10 )))
11 1. (Optional) Name a theme in the second argument to take the icon from that theme rather than the wiki's, and pass ##false## as a third argument to get an empty result instead of a fallback when the theme or the icon is missing(((
12 {{code language="velocity"}}
13 $services.icon.render('home', 'Silk')
14 $services.icon.render('home', 'Silk', false)
15 {{/code}}
16 )))
17 1. (Optional) Read the icon's metadata instead when your own code builds the markup, and pull the theme's resources yourself, which ##getMetaData## does not do(((
18 {{code language="velocity"}}
19 $services.icon.use($services.icon.currentIconSetName)
20 #set ($metadata = $services.icon.getMetaData('home'))
21 #if ($metadata.iconSetType == 'IMAGE')
22 {{html}}<img src="$metadata.url" alt=""/>{{/html}}
23 #elseif ($metadata.iconSetType == 'FONT')
24 {{html}}<span class="$metadata.cssClass" aria-hidden="true"></span>{{/html}}
25 #end
26 {{/code}}
27 )))
28 1. Save the page. The icons are drawn by the [[Icon Theme>>doc:documentation.xs.admin.look-feel.icons.icon-themes.WebHome]] the wiki uses, as follows:(((
29 {{image reference="icon-script-result.png" size="medium" alt="A saved page rendering a house icon in a sentence and in a button labelled Home, both highlighted"/}}
30 )))

Get Connected