zpt
Version:
Zenon Page Templates - JS (ZPT-JS)
237 lines (210 loc) • 13 kB
HTML
<html lang="en">
<head>
<meta charset="utf-8">
<title>Expressions</title>
<script type="text/javascript" src="../lib/zpt.min.js" defer></script>
<script type="text/javascript" src="../js/zpt.js" defer></script>
<script type="text/javascript" src="../lib/syntaxHighlighter/lib.js"></script>
<link rel="stylesheet" type="text/css" href="../docs.css">
<link rel="stylesheet" type="text/css" href="../lib/syntaxHighlighter/theme.css">
</head>
<body>
<div data-use-macro="'page@templates.html'">
<div data-fill-slot="'page-header'">
<h1>ZPT-JS tutorial - Expressions</h1>
<ul>
<li><a href="#intro">Intro</a>.</li>
<li><a href="#path">Path expressions</a>.</li>
<li><a href="#arithmethic">Arithmethic expressions</a>.</li>
<li><a href="#boolean">Boolean expressions</a>.</li>
<li><a href="#comparison">Comparison expressions</a>.</li>
<li><a href="#scripting">Scripting expressions</a>.</li>
<li><a href="#misc">Misc expressions</a>.</li>
</ul>
</div>
<article data-fill-slot="'article'">
<h2 data-attributes="id 'intro'">Intro</h2>
<p>
Do you remember how we can replace the content of any tag? Take a look at this template:
</p>
<strong>sample.html</strong>
<pre class="brush: html; highlight: [4]">
<html>
<body>
<p>
This is <span data-content="title">the title</span>.
</p>
</body>
</html>
</pre>
<p>
<em>title</em> in the <em>data-content</em> attribute is an <strong>expression</strong>. An <em>expression</em> can be evaluated by ZPT-JS to a value: this value is used by the <em>data-content</em> attribute to replace its contents. Expressions are used widely by the tags of ZPT-JS.
</p>
<h2 data-attributes="id 'path'">Path expressions</h2>
<p>
There are several types of expressions. The <em>title</em> is a <strong>variable expression</strong>, a subtype of <strong>path expressions</strong>. ZPT-JS evaluates it replacing it by its value in the dictionary (really in the scope). Very simple!
</p>
<p>
<strong>Literal expressions</strong> are also very simple. They represent constant values:
</p>
<pre class="brush: html">
<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>
</pre>
<p>
<p>
Every <strong>path expression</strong> 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!
</p>
<pre class="brush: html">
<span data-content="user/name">any friends?</span>
</pre>
<p>
To evaluate <em>user/name</em>, ZPT-JS evaluates first <em>user</em> as an object and then searchs its <em>name</em> property. This is a <strong>property expression</strong>.
</p>
<pre class="brush: html">
<span data-content="user/name | 'no friends'">any friends?</span>
</pre>
<p>
This example will be evaluated to <em>'no friends'</em> if <em>user/name</em> is evaluated to false. So this allows to set default values.
</p>
<pre class="brush: html">
<span data-content="user/birth()">birth</span>
</pre>
<p>
To evaluate <em>user/birth()</em>, ZPT-JS identifies first <em>user</em> as an object, then searchs its <em>birth</em> method, then evaluates their arguments (in this case there is none) and run it. This is a <strong>method expression</strong>.
</p>
<p>
Path expressions can be as complex as you wish:
</p>
<pre class="brush: html">
<span data-content="user/getData('personal')/day">a day</span>
</pre>
<p>
ZPT-JS evaluates first <em>user</em> as an object, then searchs its <em>getData</em> method, then evaluates their arguments (in this case a string literal: <em>personal</em>), run it and then get the <em>day</em> property of the resulting object.
</p>
<pre class="brush: html">
<span data-content="myFunction( 'an argument' )">birth</span>
</pre>
<p>
ZPT-JS identifies first <em>myFunction</em> as a function, then evaluates their arguments and run it. This is a <strong>function expression</strong>.
</p>
<pre class="brush: html">
<span data-content="items[0]">element 0 of items</span>
<span data-content="items[index]">element index of items</span>
</pre>
<p>
These are some examples of <strong>array expressions</strong>. The first one evaluates to the first element of the <em>items</em> array. The second one to item number <em>index</em>.
</p>
<pre class="brush: html">
<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>
</pre>
<p>
These are some examples of <strong>list expressions</strong>. 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.
</p>
<p>
The third one includes a <strong>range expression</strong>: <em>4:8:2</em>. 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.
</p>
<p>
Lists are very versatile:
</p>
<pre class="brush: html">
<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>
</pre>
<p>
Don't forget to not use spaces inside ranges!
</p>
<pre class="brush: html">
<span data-content="[ 1 : 3 ]">this will not work!</span>
</pre>
<h2 data-attributes="id 'arithmethic'">Arithmethic expressions</h2>
<p>
<strong>Arithmethic expressions</strong> define expressions using math operations:
</p>
<pre class="brush: html">
<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>
</pre>
<p>
The first 5 use these math operations: add (+:), substract (-:), multiply (*:), divide (/:) and module (%:). The last one shows a more complex expression that uses parenthesis.
</p>
<p>
Lists expressions can be used in arithmethic operations:
</p>
<pre class="brush: html">
<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>
</pre>
<h2 data-attributes="id 'boolean'">Boolean expressions</h2>
<p>
<strong>Boolean expressions</strong> define expressions using boolean operations:
</p>
<pre class="brush: html">
<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>
</pre>
<h2 data-attributes="id 'comparison'">Comparison expressions</h2>
<p>
<strong>Comparison expressions</strong> compare values:
</p>
<pre class="brush: html">
<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>
</pre>
<p>
The first 4 use these comparison operations: equals (eq:), not (not:), greater than (gt:) and lesser than (lt:). The last one uses the <em>in</em> operator (in:): it evaluates to true if the first element is included into the next elements.
</p>
<h2 data-attributes="id 'scripting'">Scripting expressions</h2>
<p>
<strong>Scripting expressions</strong> executes scripts:
</p>
<pre class="brush: html">
<span data-content="js: ${number100} + 1">must be 101</span>
</pre>
<p>
The first one is a <strong>javascript expression</strong>: ZPT-JS evaluates the expressions (starting by <em>$</em> and surrounded by <em>{}</em>) and then execute the resulting string as javascript code.
</p>
<h2 data-attributes="id 'misc'">Misc expressions</h2>
<p>
This category includes <strong>exists</strong>, <strong>string</strong> and <strong>format</strong> expressions.
</p>
<pre class="brush: html">
<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>
</pre>
<p>
The first and the second expressions are <strong>exists expressions</strong>. 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).
</p>
<p>
The third one is a <strong>string expression</strong>. They support to combine strings and other expressions to build a string.
</p>
<p>
<strong>Format expressions</strong> use components called <strong>formatters</strong> to format the evaluation of an expression. ZPT-JS includes some built-in formatters:
</p>
<pre class="brush: html">
<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>
</pre>
</article>
</div>
</body>
</html>