Debian Codegraph @

docs/users_guide/flowControl.rst

raw
1 Flow Control
2 ============
3
4
5 #for ... #end for
6 -----------------
7
8
9 Syntax:
10
11 ::
12
13 #for $var in EXPR
14 #end for
15
16 The {#for} directive iterates through a sequence. The syntax is the
17 same as Python, but remember the {$} before variables.
18
19 Here's a simple client listing:
20
21 ::
22
23 <TABLE>
24 #for $client in $service.clients
25 <TR>
26 <TD>$client.surname, $client.firstname</TD>
27 <TD><A HREF="mailto:$client.email" >$client.email</A></TD>
28 </TR>
29 #end for
30 </TABLE>
31
32 Here's how to loop through the keys and values of a dictionary:
33
34 ::
35
36 <PRE>
37 #for $key, $value in $dict.items()
38 $key: $value
39 #end for
40 </PRE>
41
42 Here's how to create list of numbers separated by hyphens. This
43 "#end for" tag shares the last line to avoid introducing a newline
44 character after each hyphen.
45
46 ::
47
48 #for $i in range(15)
49 $i - #end for
50
51 If the location of the {#end for} offends your sense of
52 indentational propriety, you can do this instead:
53
54 ::
55
56 #for $i in $range(15)
57 $i - #slurp
58 #end for
59
60 The previous two examples will put an extra hyphen after last
61 number. Here's how to get around that problem, using the {#set}
62 directive, which will be dealt with in more detail below.
63
64 ::
65
66 #set $sep = ''
67 #for $name in $names
68 $sep$name
69 #set $sep = ', '
70 #end for
71
72 Although to just put a separator between strings, you don't need a
73 for loop:
74
75 ::
76
77 #echo ', '.join($names)
78
79 #repeat ... #end repeat
80 -----------------------
81
82
83 Syntax:
84
85 ::
86
87 #repeat EXPR
88 #end repeat
89
90 Do something a certain number of times. The argument may be any
91 numeric expression. If it's zero or negative, the loop will execute
92 zero times.
93
94 ::
95
96 #repeat $times + 3
97 She loves me, she loves me not.
98 #repeat
99 She loves me.
100
101 Inside the loop, there's no way to tell which iteration you're on.
102 If you need a counter variable, use {#for} instead with Python's
103 {range} function. Since Python's ranges are base 0 by default,
104 there are two ways to start counting at 1. Say we want to count
105 from 1 to 5, and that {$count} is 5.
106
107 ::
108
109 #for $i in $range($count)
110 #set $step = $i + 1
111 $step. Counting from 1 to $count.
112 #end for
113
114
115 #for $i in $range(1, $count + 1)
116 $i. Counting from 1 to $count.
117 #end for
118
119 A previous implementation used a local variable {$i} as the repeat
120 counter. However, this prevented instances of {#repeat} from being
121 nested. The current implementation does not have this problem as it
122 uses a new local variable for every instance of {#repeat}.
123
124 #while ... #end while
125 ---------------------
126
127
128 Syntax:
129
130 ::
131
132 #while EXPR
133 #end while
134
135 {#while} is the same as Python's {while} statement. It may be
136 followed by any boolean expression:
137
138 ::
139
140 #while $someCondition('arg1', $arg2)
141 The condition is true.
142 #end while
143
144 Be careful not to create an infinite loop. {#while 1} will loop
145 until the computer runs out of memory.
146
147 #if ... #else if ... #else ... #end if
148 --------------------------------------
149
150
151 Syntax:
152
153 ::
154
155 #if EXPR
156 #else if EXPR
157 #elif EXPR
158 #else
159 #end if
160
161 The {#if} directive and its kin are used to display a portion of
162 text conditionally. {#if} and {#else if} should be followed by a
163 true/false expression, while {#else} should not. Any valid Python
164 expression is allowed. As in Python, the expression is true unless
165 it evaluates to 0, '', None, an empty list, or an empty dictionary.
166 In deference to Python, {#elif} is accepted as a synonym for {#else
167 if}.
168
169 Here are some examples:
170
171 ::
172
173 #if $size >= 1500
174 It's big
175 #else if $size < 1500 and $size > 0
176 It's small
177 #else
178 It's not there
179 #end if
180
181 ::
182
183 #if $testItem($item)
184 The item $item.name is OK.
185 #end if
186
187 Here's an example that combines an {#if} tag with a {#for} tag.
188
189 ::
190
191 #if $people
192 <table>
193 <tr>
194 <th>Name</th>
195 <th>Address</th>
196 <th>Phone</th>
197 </tr>
198 #for $p in $people
199 <tr>
200 <td>$p.name</td>
201 <td>$p.address</td>
202 <td>$p.phone</td>
203 </tr>
204 #end for
205 </table>
206 #else
207 <p> Sorry, the search did not find any people. </p>
208 #end if
209
210 See section output.oneLineIf for the one-line {#if} directive,
211 which is equivalent to Perl's and C's {?:} operator.
212
213 #unless ... #end unless
214 -----------------------
215
216
217 Syntax:
218
219 ::
220
221 #unless EXPR
222 #end unless
223
224 {#unless} is the opposite of {#if}: the text is executed if the
225 condition is { false}. Sometimes this is more convenient. {#unless
226 EXPR} is equivalent to {#if not (EXPR)}.
227
228 ::
229
230 #unless $alive
231 This parrot is no more! He has ceased to be!
232 'E's expired and gone to meet 'is maker! ...
233 THIS IS AN EX-PARROT!!
234 #end unless
235
236 You cannot use {#else if} or {#else} inside an {#unless} construct.
237 If you need those, use {#if} instead.
238
239 #break and #continue
240 --------------------
241
242
243 Syntax:
244
245 ::
246
247 #break
248 #continue
249
250 These directives are used as in Python. {#break} will exit a {#for}
251 loop prematurely, while {#continue} will immediately jump to the
252 next iteration in the {#for} loop.
253
254 In this example the output list will not contain "10 -".
255
256 ::
257
258 #for $i in range(15)
259 #if $i == 10
260 #continue
261 #end if
262 $i - #slurp
263 #end for
264
265 In this example the loop will exit if it finds a name that equals
266 'Joe':
267
268 ::
269
270 #for $name in $names
271 #if $name == 'Joe'
272 #break
273 #end if
274 $name - #slurp
275 #end for
276
277 #pass
278 -----
279
280
281 Syntax:
282
283 ::
284
285 #pass
286
287 The {#pass} directive is identical to Python {pass} statement: it
288 does nothing. It can be used when a statement is required
289 syntactically but the program requires no action.
290
291 The following example does nothing if only $A is true
292
293 ::
294
295 #if $A and $B
296 do something
297 #elif $A
298 #pass
299 #elif $B
300 do something
301 #else
302 do something
303 #end if
304
305 #stop
306 -----
307
308
309 Syntax:
310
311 ::
312
313 #stop
314
315 The {#stop} directive is used to stop processing of a template at a
316 certain point. The output will show { only} what has been processed
317 up to that point.
318
319 When {#stop} is called inside an {#include} it skips the rest of
320 the included code and continues on from after the {#include}
321 directive. stop the processing of the included code. Likewise, when
322 {#stop} is called inside a {#def} or {#block}, it stops only the
323 {#def} or {#block}.
324
325 ::
326
327 A cat
328 #if 1
329 sat on a mat
330 #stop
331 watching a rat
332 #end if
333 in a flat.
334
335 will print
336
337 ::
338
339 A cat
340 sat on a mat
341
342 And
343
344 ::
345
346 A cat
347 #block action
348 sat on a mat
349 #stop
350 watching a rat
351 #end block
352 in a flat.
353
354 will print
355
356 ::
357
358 A cat
359 sat on a mat
360 in a flat.
361
362 #return
363 -------
364
365
366 Syntax:
367
368 ::
369
370 #return
371
372 This is used as in Python. {#return} will exit the current method
373 with a default return value of {None} or the value specified. It
374 may be used only inside a {#def} or a {#block}.
375
376 Note that {#return} is different from the {#stop} directive, which
377 returns the sum of all text output from the method in which it is
378 called. The following examples illustrate this point:
379
380 ::
381
382 1
383 $test[1]
384 3
385 #def test
386 1.5
387 #if 1
388 #return '123'
389 #else
390 99999
391 #end if
392 #end def
393
394 will produce
395
396 ::
397
398 1
399 2
400 3
401
402 while
403
404 ::
405
406 1
407 $test
408 3
409 #def test
410 1.5
411 #if 1
412 #stop
413 #else
414 99999
415 #end if
416 #end def
417
418 will produce
419
420 ::
421
422 1
423 1.5
424 3
425
426