Templates
Templates are a way to reuse configured complex components which differ in simple ways. Define them once, reuse them multiple times by defining only what's unique about each.
Templates can be defined in the templates object of the ui.json, or as separate files.
When stored in the templates object, a template is keyed by name:
"templates": {
"mybutton": {
"type": "button",
"text": "My Button"
}
}
When stored in a file, templates are accessed by relative filename e.g. templates/mybutton.json, so simply add the component object to the file:
{
"type": "button",
"text": "My Button"
}
Picking between using the templates object and using separate template files is left to your prerogative.
Template Parameters
The magic of a template, and what enables the reusability, is parameters.
"templates": {
"mybutton": {
"type": "button",
"text": "{param:text}"
}
}
The parameters within a template can be accessed via a params key when using a template.
NoteTemplate parameters are immediately substituted on using the template.
Using a Template
Templates are used within the components array of a view, and can be used alongside other components and templates.
Properties
- params
- OBJECT
The parameter values as key-value pairs, used to set the {param:} values within the template.
Example
{
"type": "view",
"components": [
{
"type": "template",
"src": "mybutton",
"params": {
"text": "This is a reusable button"
}
}
]
}
If the template is within a file, set the src to the relative path, e.g.
"src": "templates/mybutton.json"
Overriding constraints and id
If constraints or an id are set within a template, these will be overridden by the JSON using the template, allowing component templates to act like normal components.
ImportantIt is good practice to avoid setting any
idkeys within a template, as this will break the globally unique ids throughout the UI rule.