source: documentation/data.tpl.processor @ 1406:6b099cf4ab8d

Revision 1406:6b099cf4ab8d, 5.8 KB checked in by niam, 3 years ago (diff)

changed directory layout

Line 
1= libdodo template processor =
2
3Template processor supports simple but yet enough powerful language.
4
5It supports includes, variables, conditional statements, loops, namespaces, etc.
6
7The primary goal of the template processor is to separate application code from its presentation.
8
9== syntax ==
10
11=== template language blocks ===
12Each part of template that the processor will parse is enclosed into special open and close statements.
13Text outside the template blocks is not being parsed and is being sent to the output without any modifications.
14Text in statements block is always treated as text if it is not a variable.
15Spaces are trimmed out in the beginning and in the end of the text block.
16If text area is enclosed with quotes or double quotes the quotes are being removed from the output and text inside is being pushed to the output without any further modifications.
17
18 * Processing block
19
20   Processor assumes that some language statement is enclosed in this block
21
22   <( - open statement
23
24   )> - close statement
25
26   For example:
27
28   <(for $c in $array)> <(print $c)> <(rof)>
29
30 * Non-parsed block
31
32   Processor does not parse the block enclosed with these statements and sends to the output as-is
33
34   <(> - beginning of the non-parsing area
35
36   <)> - end of the non-parsing area
37
38   For example:
39
40   <(>
41        <(print a=, $a,  b=, $b)>
42
43        <(if $a!=$b)>
44                a != b
45        <(else)>
46                a == b
47        <(fi)>
48   <)>
49
50
51 * Comment block
52
53   Comment block is thrown out from the processor output
54
55   <(* - beginning of the comment
56
57   *)> - end of the comment
58
59   For example:
60
61   <(* comment here *)>
62
63
64=== variables ===
65Names of the variables begin with '$' sign.
66
67For example:
68
69$var
70$1var
71
72
73Variable is resolved only within the processor statement and if it is not enclosed with any quotes. Text in the quotes is not being interpolated and variables are not being resolved in it.
74
75Processor supports 4 types of variables:
76 1. string
77 2. array
78 3. hash
79 4. array of hashes
80
81Variable is iterative with 'for' loop and variable member can be accessed directly with '.' operator.
82
83For example:
84 1. first symbol of string:
85
86    $string.0
87
88 2. second element in array:
89
90    $array.1
91
92 3. first symbol of second element in array:
93
94    $array.1.0
95
96 4. element named 'one' of hash:
97
98    $hash.one
99
100 5. element named 'one' of second element in array of hashes:
101
102    $hashArray.1.one
103
104
105Variable can be used as a key to the member. In this case it must be enclosed in '{}' brackets.
106For example:
107 1. element named 'one' of second element in array of hashes(variable 'variable' has value 'one'):
108
109    $hashArray.1.{$variable}
110
111 2. element named 'one' of second element in array of hashes(array 'hash' has member 'one' which value is 'one'):
112
113    $hashArray.1.{$hash.one}
114
115
116=== namespaces ===
117Variables have scope of visibility in the statement block.
118
119For example:
120
121<(for $b in $arr2)>
122        <(for $b in $b)>
123                <(print $b)>
124        <(rof)>
125<(rof)>
126
127
128<(assign a = test1)>
129
130<(if true)>
131        <(assign a = test2)>
132        <(print a= ', $a,' (must be equal to 'test2'))>
133<(fi)>
134<(print a= ', $a,' (must be equal to 'test1'))>
135
136
137=== statements ===
138
139==== ns ====
140'ns' statement explicitly defines namespace scope.
141
142For example:
143
144<(assign a = test1)>
145<(ns)>
146        <(ns)>
147                <(assign a = in namespace)>
148                <(print a= ', $a,' (must be equal to 'in namespace'))>
149        <(sn)>
150        <(print a= ', $a,' (must be equal to 'test1'))>
151<(sn)>
152
153
154==== include ====
155'include' statement allows to load template from external file.
156
157Included template file is processed and the contents of the processed template is inserted at the point of inclusion.
158
159'include' takes string or variable as an argument. Path to template is relative to defined in tpl::processor object instance.
160
161For example:
162
163<(include $template)>
164
165<(include template.tpl)>
166
167
168==== if ====
169'if' is a conditional statement that allows or discards processing of the template block.
170
171'else' statement defines template block that is processed if conditional statement in 'if' is 'false'.
172
173'if' allows to use only one comparison per block. There is no logical 'OR' or 'AND'.
174
175Conditional statement for 'if' allows using '==', '!' and '!=' to compare strings, and '==', '!=', '!', '<=', '>=', '<', '>' to compare numeric values.
176
177Unary operator '!' turns statement into 'false' if statement is an empty string or zero for numeric or string that is equal to 'false'.
178
179For example:
180
181<(if $a == $b)>
182        $a equal to $b
183<(fi)>
184
185<(if $b)>
186        $b is not 'false', not empty string nor zero
187<(fi)>
188
189<(if ! $a)>
190        <(if $a == false)>
191                $a is 'false'
192        <(else)>
193                <(if $a == 0)>
194                        $a is zero
195                <(else)>
196                        $a is an empty string
197                <(fi)>
198        <(fi)>
199<(else)>
200        smth else ...
201<(fi)>
202
203
204==== for ====
205'for' statement is used to iterate over string, array of objects or hash.
206
207Template processor understands two kinds of 'for' statements:
208 * Iterate obtaining values of elements:
209
210   <(for $value in $array)><(rof)>
211
212 * Iterate obtaining pairs of (key, value)
213
214   For hash 'key' is a hash key, otherwise it is an index of element in array or string.
215
216   <(for $key => $value in $hash)><(rof)>
217
218
219'break' statement inside 'for' block stops the current loop. If numeric value is specified for 'break' statement, amount of loops specified as 'break' argument would be stopped upwards.
220
221'continue' statement inside 'for' block starts new iteration of the loop.
222
223Special variable '$dodo.iterator' stands as a counter for current loop.
224
225For example:
226
227<(for $value in $array)>
228    <(if $dodo.iterator > 10)>
229        <(break)>
230    <(fi)>
231<(rof)>
232
233
234
235<(for $key => $value in $hash)>
236    <(for $value in $value)>
237        <(if $value == needle)>
238            <(break 2)>
239        <(fi)>
240    <(rof)>
241<(rof)>
242
243
244==== print ====
245
246'print' statement pushes variable value to the output.
247
248'print' accepts more than one argument. 'print' arguments should be delimited with coma.
249
250For example:
251
252<(print $array.{0}.{$key})>
253
254<(print $array.1.{$hash.{$key}})>
255
256<(print $array.{0}.{$key}, "and ", $array.1.{$hash.{$key}})>
Note: See TracBrowser for help on using the repository browser.