Debian Codegraph @

docs/dev_guide/flowControl.rst

raw
1 Directives: Flow Control
2 ========================
3
4
5 #for
6 ----
7
8
9 The template:
10
11 ::
12
13 #for $i in $range(10)
14 $i #slurp
15 #end for
16
17 The output:
18
19 ::
20
21 0 1 2 3 4 5 6 7 8 9
22
23 The generated code:
24
25 ::
26
27 for i in range(10):
28 write(filter(i)) # generated from '$i' at line 2, col 1.
29 write(' ')
30
31 #repeat
32 -------
33
34
35 The template:
36
37 ::
38
39 #repeat 3
40 My bonnie lies over the ocean
41 #end repeat
42 O, bring back my bonnie to me!
43
44 The output:
45
46 ::
47
48 My bonnie lies over the ocean
49 My bonnie lies over the ocean
50 My bonnie lies over the ocean
51 O, bring back my bonnie to me!
52
53
54 The generated code:
55
56 ::
57
58 for __i0 in range(3):
59 write('My bonnie lies over the ocean\n')
60 write('O, bring back my bonnie to me!\n')
61
62 Note that a new local variable of the form {\_\_i$num} will be used
63 for each instance of {repeat} in order to permit nesting.
64
65 #while
66 ------
67
68
69 The template:
70
71 ::
72
73 #set $alive = True
74 #while $alive
75 I am alive!
76 #set $alive = False
77 #end while
78
79 The output:
80
81 ::
82
83 I am alive!
84
85 The generated code:
86
87 ::
88
89 alive = True
90 while alive:
91 write('I am alive!\n')
92 alive = False
93
94 #if
95 ---
96
97 ()
98
99 The template:
100
101 ::
102
103 #set $size = 500
104 #if $size >= 1500
105 It's big
106 #else if $size < 1500 and $size > 0
107 It's small
108 #else
109 It's not there
110 #end if
111
112 The output:
113
114 ::
115
116 It's small
117
118 The generated code:
119
120 ::
121
122 size = 500
123 if size >= 1500:
124 write("It's big\n")
125 elif size < 1500 and size > 0:
126 write("It's small\n")
127 else:
128 write("It's not there\n")
129
130 #unless
131 -------
132
133
134 The template:
135
136 ::
137
138 #set $count = 9
139 #unless $count + 5 > 15
140 Count is in range.
141 #end unless
142
143 The output:
144
145 ::
146
147 Count is in range.
148
149 The generated code:
150
151 ::
152
153 count = 9
154 if not (count + 5 > 15):
155 write('Count is in range.\n')
156
157 { Note:} There is a bug in Cheetah 0.9.13. It's forgetting the
158 parentheses in the {if} expression, which could lead to it
159 calculating something different than it should.
160
161 #break and #continue
162 --------------------
163
164
165 The template:
166
167 ::
168
169 #for $i in [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 'James', 'Joe', 'Snow']
170 #if $i == 10
171 #continue
172 #end if
173 #if $i == 'Joe'
174 #break
175 #end if
176 $i - #slurp
177 #end for
178
179 The output:
180
181 ::
182
183 1 - 2 - 3 - 4 - 5 - 6 - 7 - 8 - 9 - 11 - 12 - James -
184
185 The generated code:
186
187 ::
188
189 for i in [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 'James', 'Joe', 'Snow']:
190 if i == 10:
191 write('')
192 continue
193 if i == 'Joe':
194 write('')
195 break
196 write(filter(i)) # generated from '$i' at line 8, col 1.
197 write(' - ')
198
199 #pass
200 -----
201
202
203 The template:
204
205 ::
206
207 Let's check the number.
208 #set $size = 500
209 #if $size >= 1500
210 It's big
211 #elif $size > 0
212 #pass
213 #else
214 Invalid entry
215 #end if
216 Done checking the number.
217
218 The output:
219
220 ::
221
222 Let's check the number.
223 Done checking the number.
224
225 The generated code:
226
227 ::
228
229 write("Let's check the number.\n")
230 size = 500
231 if size >= 1500:
232 write("It's big\n")
233 elif size > 0:
234 pass
235 else:
236 write('Invalid entry\n')
237 write('Done checking the number.\n')
238
239 #stop
240 -----
241
242
243 The template:
244
245 ::
246
247 A cat
248 #if 1
249 sat on a mat
250 #stop
251 watching a rat
252 #end if
253 in a flat.
254
255 The output:
256
257 ::
258
259 A cat
260 sat on a mat
261
262 The generated code:
263
264 ::
265
266 write('A cat\n')
267 if 1:
268 write(' sat on a mat\n')
269 if dummyTrans:
270 return trans.response().getvalue()
271 else:
272 return ""
273 write(' watching a rat\n')
274 write('in a flat.\n')
275
276 #return
277 -------
278
279
280 The template:
281
282 ::
283
284 1
285 $test[1]
286 3
287 #def test
288 1.5
289 #if 1
290 #return '123'
291 #else
292 99999
293 #end if
294 #end def
295
296 The output:
297
298 ::
299
300 1
301 2
302 3
303
304 The generated code:
305
306 ::
307
308 def test(self,
309 trans=None,
310 dummyTrans=False,
311 VFS=valueFromSearchList,
312 VFN=valueForName,
313 getmtime=getmtime,
314 currentTime=time.time):
315
316
317 """
318 Generated from #def test at line 5, col 1.
319 """
320
321 if not trans:
322 trans = DummyTransaction()
323 dummyTrans = True
324 write = trans.response().write
325 SL = self._searchList
326 filter = self._currentFilter
327 globalSetVars = self._globalSetVars
328
329 ########################################
330 ## START - generated method body
331
332 write('1.5\n')
333 if 1:
334 return '123'
335 else:
336 write('99999\n')
337
338 ########################################
339 ## END - generated method body
340
341 if dummyTrans:
342 return trans.response().getvalue()
343 else:
344 return ""
345
346 ::
347
348 def respond(self,
349 trans=None,
350 dummyTrans=False,
351 VFS=valueFromSearchList,
352 VFN=valueForName,
353 getmtime=getmtime,
354 currentTime=time.time):
355
356
357 """
358 This is the main method generated by Cheetah
359 """
360
361 if not trans:
362 trans = DummyTransaction()
363 dummyTrans = True
364 write = trans.response().write
365 SL = self._searchList
366 filter = self._currentFilter
367 globalSetVars = self._globalSetVars
368
369 ########################################
370 ## START - generated method body
371
372 write('\n1\n')
373 write(filter(VFS(SL,"test",1)[1])) # generated from '$test[1]' at line 3, col 1.
374 write('\n3\n')
375
376 ########################################
377 ## END - generated method body
378
379 if dummyTrans:
380 return trans.response().getvalue()
381 else:
382 return ""
383
384