Conditions are used to execute actions or apply changes only when specific criteria are met. They allow the behavior and appearance of an app to adapt dynamically to data, user input, and other conditions.
In mos., conditions can be used in the following contexts:
- Layer value conditions: Change the value of a layer depending on a condition.
- Layer state conditions: Apply a specific layer state when a condition is met.
- Event conditions: Execute different actions depending on a condition within an event.
- If-Else conditions: Execute different actions depending on a condition within a list of actions.
Conditions are based on evaluables.
Quick guide
A condition consists of three main properties:
| Property | Description |
|---|
mode | Defines the comparison operator. |
left | Defines the value to check. |
right | Defines the value to compare against. |
For example, the following condition checks whether viewCount is greater than 0:
"conditions": {
"mode": "greaterThan",
"left": "{{viewCount}}",
"right": "0"
}
The condition evaluates to true if viewCount is greater than 0, and to false otherwise.
Conditions can be extended with additional properties depending on where they are used.
Using conditions
Layer value conditions
Layer value conditions allow the value of a layer to change dynamically based on a condition.
They are defined in the value field of a layer within a client template.
For example, the following configuration displays a singular or plural version of the word "view", depending on the value of viewCount:
{
"name": "detailViewTemplate",
"content": {
"content": {
"viewCountText": {
"value": {
"left": "{{viewCount}}",
"mode": "equals",
"right": "1",
"trueValue": "{{viewCount}} view",
"falseValue": "{{viewCount}} views"
}
}
}
}
}
The trueValue and falseValue properties define the values returned depending on the result of the condition:
trueValue: Returned if the condition evaluates to true.
falseValue: Returned if the condition evaluates to false.
Both properties accept either a string or an evaluable.
In this example, the layer displays "1 view" when viewCount equals 1. For all other values, it displays the plural form, such as "0 views" or "2 views".
This is particularly useful for dynamically changing the content of a layer without having to define multiple layers or additional actions.
Layer value conditions only affect the value field of a layer. If other layer properties need to change, use layer state conditions instead.
Layer state conditions
Layer state conditions allow layer states to be applied dynamically depending on a condition.
Unlike layer value conditions, they can change multiple properties of a layer at once. For example, they can be used to hide a layer, change its opacity, or modify its appearance.
A layer state condition consists of:
- A state defined in the
states object.
- A condition defined in the
conditions array.
- A reference to the state that should be applied when the condition is met.
The following example hides the text layer viewCountText whenever viewCount equals 0:
{
"name": "detailViewTemplate",
"content": {
"viewCountText": {
"states": {
"hidden": {
"hidden": true
}
},
"conditions": [
{
"state": "hidden",
"conditions": {
"mode": "equals",
"left": "{{viewCount}}",
"right": "0"
}
}
]
}
}
}
In this example, a state named hidden is defined in the states object. The state sets the hidden property to true.
The conditions array defines when the state should be applied. If viewCount equals 0, the hidden state is applied to the layer.
As a result, the entire viewCountText layer is hidden whenever there are no views.
Layer state conditions are particularly useful when different layer states need to be applied depending on the current data.
For example, an online shop can display an enabled purchase button when items are available and a disabled or hidden button when the item count is 0.
Event conditions
Conditions can also be used within events to execute different actions depending on whether a condition is met.
An event condition defines two possible action sequences:
actions: Executed if the condition evaluates to true.
elseActions: Executed if the condition evaluates to false.
The following example shows a global event that displays a different message depending on the value of viewCount:
{
"name": "viewCountEvent",
"content": {
"type": "viewCountEvent",
"actions": [
{
"type": "viewCountEvent",
"conditions": {
"mode": "greaterThan",
"left": {
"context": "parameter",
"field": "viewCount"
},
"right": "0"
},
"actions": [
{
"type": "showMessage",
"params": {
"text": "There have been {{viewCount}} views so far."
}
}
],
"elseActions": [
{
"type": "showMessage",
"params": {
"text": "There have been no views so far."
}
}
]
}
]
}
}
In this example, the condition accesses the value of viewCount through the parameter context.
If viewCount is greater than 0, the event executes the actions and displays the number of views. Otherwise, it executes the elseActions and displays a message indicating that there have been no views.
When an event is used inside a template, leadingDelimiter and trailingDelimiter can be used to access the data passed to the event.
For more information about passing parameters to events, see Event.
If-Else conditions
If-Else conditions provide a way to execute different actions depending on a condition without having to trigger an event first.
They can be used directly within an array of actions.
The structure is similar to event conditions, but uses the following properties:
condition: Defines the condition to evaluate.
then: Contains the actions executed if the condition evaluates to true.
else: Contains the actions executed if the condition evaluates to false.
The following example displays a different message depending on whether viewCount is greater than 0:
{
"actions": [
{
"type": "if",
"params": {
"condition": {
"mode": "greaterThan",
"left": "{{viewCount}}",
"right": "0"
},
"then": [
{
"type": "showMessage",
"params": {
"text": "There have been {{viewCount}} views so far."
}
}
],
"else": [
{
"type": "showMessage",
"params": {
"text": "There have been no views so far."
}
}
]
}
}
]
}
If viewCount is greater than 0, the actions defined in then are executed. Otherwise, the actions defined in else are executed.
Unlike event conditions, If-Else conditions are defined directly within an action list and do not require a separate event definition.