Which variables can I use in the "Custom Display" field?
Last modified by Michael Hamann (non-admin) on 2026/11/24 11:10
In a class, each property has a "Custom Display" field where you can write a script that displays the property in a custom way, like the User Property Custom Display snippet.
The core makes the following variables available to this script:
- $name = the name of the displayed property
- $prefix = a prefix for the form field: the name of the form field is ${prefix}${name}
- $object = the displayed object
- $type = the intended display mode (view, edit, hidden, search...)
- $value = the property value, which is a list for a property that allows multiple selection
- $field = the definition of the property (in the class), of type com.xpn.xwiki.api.PropertyClass. Some of its properties, like name, prettyName or type, are directly accessible; the others are retrieved with $field.getProperty('property_name').value.
- $aria-label = (since XWiki 16.10.3 and 17.0.0) a text alternative that tells what the input is about, for the case where the input isn't labelled in another way
For example, for a Page property (or a database list) that allows a single selection, you may want:
- in edit mode, a select list to choose a page, or nothing
- in view mode, a link to the chosen page, or nothing
Here's a "Custom Display" that does this:
{{velocity}}
#if ($type == 'edit')
#set ($fieldName = "${prefix}${name}")
{{html}}
<select id="$escapetool.xml($fieldName)" name="$escapetool.xml($fieldName)" aria-label="$!escapetool.xml($aria-label)">
<option value=""#if ("$!value" == '') selected="selected"#end>---</option>
#foreach ($listItem in $field.getListValues())
<option value="$escapetool.xml($listItem)"#if ($listItem == $value) selected="selected"#end>$escapetool.xml($listItem)</option>
#end
</select>
## Submits an empty value, the way the core list displayers do, so that the property can be cleared.
<input type="hidden" name="$escapetool.xml($fieldName)" value=""/>
{{/html}}
#elseif ($type == 'view' && "$!value" != '')
[[$services.rendering.escape($value, 'xwiki/2.1')]]
#else
$!services.rendering.escape($value, 'xwiki/2.1')
#end
{{/velocity}}The script escapes every value it outputs: $escapetool.xml for the values written into HTML and $services.rendering.escape for the values written into wiki syntax. Without this, a value containing quotes, HTML or wiki syntax breaks the form or injects markup into the page.
Of course, you can use similar code in a sheet to display the data the way you want.