Intro
Do you remember how we can replace the content of any tag? Take a look at this template:
sample.html<html> <body> <p> This is <span data-content="title">the title</span>. </p> </body> </html>
title in the data-content attribute is an expression. An expression can be evaluated by ZPT-JS to a value: this value is used by the data-content attribute to replace its contents. Expressions are used widely by the tags of ZPT-JS.
Path expressions
There are several types of expressions. The title is a variable expression, a subtype of path expressions. ZPT-JS evaluates it replacing it by its value in the dictionary (really in the scope). Very simple!
Literal expressions are also very simple. They represent constant values:
<span data-content="'this is a string literal'">a string literal</span> <span data-content="123">an integer number</span> <span data-content="123.45">a float number</span> <span data-content="true">a boolean true value</span> <span data-content="false">a boolean false value</span>
Every path expression starts with a variable name. The available variable names refer either to objects or variables defined in the dictionary or within the Page Template. Let's see some examples of path expressions!
<span data-content="user/name">any friends?</span>
To evaluate user/name, ZPT-JS evaluates first user as an object and then searchs its name property. This is a property expression.
<span data-content="user/name | 'no friends'">any friends?</span>
This example will be evaluated to 'no friends' if user/name is evaluated to false. So this allows to set default values.
<span data-content="user/birth()">birth</span>
To evaluate user/birth(), ZPT-JS identifies first user as an object, then searchs its birth method, then evaluates their arguments (in this case there is none) and run it. This is a method expression.
Path expressions can be as complex as you wish:
<span data-content="user/getData('personal')/day">a day</span>
ZPT-JS evaluates first user as an object, then searchs its getData method, then evaluates their arguments (in this case a string literal: personal), run it and then get the day property of the resulting object.
<span data-content="myFunction( 'an argument' )">birth</span>
ZPT-JS identifies first myFunction as a function, then evaluates their arguments and run it. This is a function expression.
<span data-content="items[0]">element 0 of items</span> <span data-content="items[index]">element index of items</span>
These are some examples of array expressions. The first one evaluates to the first element of the items array. The second one to item number index.
<span data-content="[1 5 99 number]">1, 5, 99 and number</span> <span data-content="['a' 'b' 99 number]">'a', 'b', 99 and number</span> <span data-content="[4:8:2 number]">4, 6, 8 and number</span>
These are some examples of list expressions. The first one evaluates to an array with elements 1, 5, 99 and number. Mixing elements of different type is supported: the second one evaluates to an array with elements 'a', 'b', 99 and number.
The third one includes a range expression: 4:8:2. The first argument (4) defines the number to start, the second one (8) the number to stop and the third one (2) the step. It evaluates to 4, 6, 8. So the full expression evaluates to 4, 6, 8 and number.
Lists are very versatile:
<span data-content="[1:3 10]">evaluates to 1,2,3,10</span> <span data-content="[4 1:3 10]">evaluates to 4,1,2,3,10</span>
Don't forget to not use spaces inside ranges!
<span data-content="[ 1 : 3 ]">this will not work!</span>
Arithmethic expressions
Arithmethic expressions define expressions using math operations:
<span data-content="+: 5 4">must be 9</span> <span data-content="-: 5 4">must be 1</span> <span data-content="*: 5 4">must be 20</span> <span data-content="/: 69 3">must be 23</span> <span data-content="%: 23 7">must be 2</span> <span data-content="*: 2 ( +: number100 1 )">must be 202</span>
The first 5 use these math operations: add (+:), substract (-:), multiply (*:), divide (/:) and module (%:). The last one shows a more complex expression that uses parenthesis.
Lists expressions can be used in arithmethic operations:
<span data-content="+: [1:3 10]">evaluates to 1+2+3+10 = 16</span> <span data-content="+: [4 1:3 10]">evaluates to 4+1+2+3+10 = 20</span>
Boolean expressions
Boolean expressions define expressions using boolean operations:
<span data-content="and: somethingTrue somethingFalse">must be false</span> <span data-content="or: somethingTrue somethingFalse">must be true</span> <span data-content="not: somethingTrue">must be false</span> <span data-content="cond: somethingFalse 'It is true!' 'Liar!'">must be Liar!</span>
Comparison expressions
Comparison expressions compare values:
<span data-content="eq: 1 number1">must be true</span> <span data-content="not: eq: 1 number1">must be false</span> <span data-content="gt: 10 number1">must be true</span> <span data-content="lt: 10 number1">must be false</span> <span data-content="in: 1 0 1">must be true</span> <span data-content="in: 1 0 2">must be false</span>
The first 4 use these comparison operations: equals (eq:), not (not:), greater than (gt:) and lesser than (lt:). The last one uses the in operator (in:): it evaluates to true if the first element is included into the next elements.
Scripting expressions
Scripting expressions executes scripts:
<span data-content="js: ${number100} + 1">must be 101</span>
The first one is a javascript expression: ZPT-JS evaluates the expressions (starting by $ and surrounded by {}) and then execute the resulting string as javascript code.
Misc expressions
This category includes exists, string and format expressions.
<span data-content="exists: existingVar">must be true</span> <span data-content="exists: non/existing/path">must be false</span> <span data-content="string:user is ${user/name}">must be user is Bob</span>
The first and the second expressions are exists expressions. The evaluate to true if the arguments is not undefined (the first one). If a path is provided it evaluates the full path must exist to be evaluated as true (the second one).
The third one is a string expression. They support to combine strings and other expressions to build a string.
Format expressions use components called formatters to format the evaluation of an expression. ZPT-JS includes some built-in formatters:
<span data-content="format: lowerCase 'Test'">must be test</span> <span data-content="format: upperCase 'Test'">must be TEST</span> <span data-content="format: fix 1.371 2">must be 1.37</span> <span data-content="format: fix (/: 1 3) 2">must be 0.33</span>