$for i in range(10):
I like $i
$for i in range(10): I like $i
$while a:
hello $a.pop()
$if times > max:
Stop! In the name of love.
$else:
Keep on, you can do it.
在循环中可以访问的一些内置变量
loop.index: the iteration of the loop (1-indexed)
loop.index0: the iteration of the loop (0-indexed)
loop.first: True if first iteration
loop.last: True if last iteration
loop.odd: True if an odd iteration
loop.even: True if an even iteration
loop.parity: "odd" or "even" depending on which is true
loop.parent: the loop above this in nested loops
例如下面
<table>
$for c in ["a", "b", "c", "d"]:
<tr class="$loop.parity">
<td>$loop.index</td>
<td>$c</td>
</tr>
</table>
你还可以定义方法
$code:
x = "you can write any python code here"
y = x.title()
z = len(x + y)
def limit(s, width=10):
"""limits a string to the given width"""
if len(s) >= width:
return s[:width] + "..."
else:
return s
And we are back to template.
The variables defined in the code block can be used here.
For example, $limit(x)
当然也可以使用layout