Dynamic expressions
The UI Framework implements the concept of self-updating configurations where, instead of passing a value, you can put a reference to, say, the value of the option, and each time this option is updated, the value is overwritten. This appears as follows:
{
"title": "{{ $['corner-type'].selected.title }}"
}
Here, the value of the title property is obtained at runtime. This element corresponds to the title of a currently selected value in the widget called corner-type.
If you are familiar with HTML template engines, like Razor, Jade, Mustache, etc., you may find it very similar. The only difference is that it is applied to JSON, not HTML. However, in the UI Framework, template engine capabilities are quite standard. It supports:
- Getting a value
- Conditional expressions
- Loops
- Variables
- Merge/concat objects
What parts of the editor config can be dynamic?
At the moment, this only works with widget parameters. You can use dynamic params as follows:
{
"widgets": [{
"type": "static-text",
"name": "team",
"params": {
"text": "{{ $['team-selector'].selected.title}}"
}
}]
}
However, if you try to write the dynamic part outside of the params, it won't work. The following example does not work:
{
"widgets": [{
"type": "static-text",
"name": "team",
"title": "{{ $['team-selector'].selected.title}}",
"params": {
...
}
}]
}
You can not reference a widget within the same widget. However, you may use the self variable inside the expression.
How does this work?
When getting the config, the editor looks for dynamic parts in the config. This can be one of:
- A string value containing double curly braces, for example,
'something': '{{js-code}}' - An object containing a key with double curly braces, for example,
'something': {'{{#if condition}}': 'value'}
The editor executes the code inside braces or special expressions starting with #, and then replaces the dynamic part with the result. Also, it spots the parts that can be changed at runtime, such as references to variables or widgets, and subscribes to their changes. When such changes occur, it reassigns the corresponding values in the config.
For the complete syntax of dynamic expressions, see Dynamic expression syntax.