VDL Expression Language Syntax
There are several basic rules for the VDL expression language.
Expressions are case sensitive - Return and return refer to different objects.
The dot operator is used to access properties of objects. For example:
scenario.entities.Return.valuegives access to the entities property of the scenario object. This in turn is also an object, and the second dot provides access to the Return property of the entities object.
Array Access
Elements within a VDL expression language array are accessed using square brackets, inside which the array index of the required element is specified (remember that array indices start at 0) - the following snippet shows how to access the 2nd element of the array.
scenarios[1]When you dereference a Mosel array entity within a VDL expression, you will need to follow an established rule - because array entities can have multiple indices they cannot be treated as plain arrays, and you need instead to call the array name as a function, passing the indices as parameters.
scenario.entities.MyArray(0).value
Warning: It is unsafe to reference array elements directly without guarding the expression to check that the element actually exists. Usually, you will reference elements of an array by looping over all of the indices. To guard access to a specific element you can use ternary expressions such as:
For more detail about accessing array and other entities, see
Entity Values and Labels.
<span vdl-text="=scenario.entities.MyArray(0) ? scenario.entities.MyArray(0).value : ''"></span>
Function Calls
Functions of the VDL expression language can be written inline or within script blocks.
Inline functions are written within an expression itself:
<vdl-validate pass="=function(entity,value) { return value > 100; }"> </vdl-validate>
![]() |
Note See
Using Validators for information about the
<vdl-validate> element.
|
Inline functions are appropriate if a function is simple and doesn't need to be shared. Another option is to write it in a
script block:
<script> function MyFunc (entity, value) { return value < 10; } </script> <vdl-validate pass="=MyFunc"></vdl-validate>This method provides more space in which to write what might be a more complex function, it can be read and understood more clearly and permits sharing among many VDL elements if needed.
String Demarcation
You will often wish to use strings within expressions - perhaps to concatenate them with other strings or returned entity values. Recall that an expression is already enclosed by a pair of double quotes, so it is essential to use single quotes for strings within expressions:
<vdl-column vdl-text="='a'+'bb'+'ccc'"></vdl-column>The code snippet above would return the value as ' abbcc'.
Key-value pairs
Several VDL attributes take key-value pairs as their input. For example:
<div vdl-css="hilite: true, showing: false"></div>The symbol before a colon is called a key, anything after it (up until the end of the attribute string or the next comma) is the value. These are treated as evaluated expressions. If a chosen key contains anything other than alphanumeric characters (a-z, A-Z and 0-9) then the key itself must be placed inside quotes. To avoid clashing with the attribute quotes, use single quotes:
<div vdl-css="'hilite-item': true, 'showing-item':false"></div>Similarly, when the value is a string, for the same reason, enclose it in single quotes.
Arithmetic and Comparision (Boolean) Operators
A range of standard and boolean operators is available.
Operator | Name | Example |
---|---|---|
+ | add | 2+2 = 4 |
- | unary negative | -10 |
- | subtract | 12 - 7 = 4 |
* | multiply | 8 * 7 = 56 |
/ | divide | 20/4 = 5 |
% | remainder | 20 % 7 = 6 |
< | less than | 3 < 6 |
<= | less than or equal to | 5 <= 10 |
> | greater than | 17 > 1 |
>= | greater than or equal to | 20 >= 20 |
== | roughly equal to | 3 == '3' |
!= | roughly not equal to | 4 != '4' |
=== | strictly equal | 6 === 6 |
!== | strictly not equal to | 6 !== '6' |
![]() |
Note If in doubt, use the strict comparisons. Use of the roughly equal/not equal operators can lead to unexpected matches.
|
Logical Operators
The VDL expression language implements three logical operators.
Operator | Name | Example |
---|---|---|
! | not | !false === true |
&& | logical and | True && !true === false |
|| | logical or | false || true === true |