XPages Tip: Overriding, Concatenating, and Conditionally Adding Control Properties via a Theme
Themes are commonly-used in XPages applications to include resources needed throughout an application, but they can also be a powerful tool for consistently applying application-wide control settings. In this post, I’ll explain the difference between different modes for applying a control property and link to a corresponding post with a practical example.
1. Overriding a Control Property
In this post, I showed how to use the theme to add a class to all labels in an application to pick up Bootstrap styling without having to set the class on each individual label.
Here’s an example of the theme code to make it happen:
<control> <name>Text.Label</name> <property mode="override"> <name>styleClass</name> <value>control-label</value> </property> </control>
When a page loads, the theme is applied to the page and all label controls are updated as the page is rendered.
- Line 2 is directing it to apply to all Label controls throughout the application. (This is using a theme ID that’s predefined for the label control, as noted in the Mastering XPages book.)
- Line 4 specifies that it’s updating the
styleClassproperty of the controls.
- Line 5 specifies the
control-labelclass to add to each label. (This is the class name that Bootstrap styling looks for.)
- Line 3 is the variable for this post. This example starts with the setting the override mode. This mode will set the class to the specified value on all label controls, overriding anything that may have been set when the control was added on an XPage or custom control.
2. Concatenating a Control Property
Instead of overriding the property on all controls, another option is to concatenate the specified value to any existing value specified on each instance of the control.
In this case, if there was a label control that already had a class specified, the
control-label class would be added to it (after a space) so the label would have both classes.
If the label control did not have a class, then the specified class would be added.
3. Conditionally Applying a Control Property
The last option is not to specify a mode at all.
In this case, it will not do anything to a label control that already has a class specified, but it will add the specified class to any label control that does not have one.