| 1 |
Directives: Output |
| 2 |
================== |
| 3 |
|
| 4 |
|
| 5 |
#echo |
| 6 |
----- |
| 7 |
|
| 8 |
|
| 9 |
The template: |
| 10 |
|
| 11 |
:: |
| 12 |
|
| 13 |
Here is my #echo ', '.join(['silly']*5) # example |
| 14 |
|
| 15 |
The output: |
| 16 |
|
| 17 |
:: |
| 18 |
|
| 19 |
Here is my silly, silly, silly, silly, silly example |
| 20 |
|
| 21 |
The generated code: |
| 22 |
|
| 23 |
:: |
| 24 |
|
| 25 |
write('Here is my ') |
| 26 |
write(filter(', '.join(['silly']*5) )) |
| 27 |
write(' example\n') |
| 28 |
|
| 29 |
#silent |
| 30 |
------- |
| 31 |
|
| 32 |
|
| 33 |
The template: |
| 34 |
|
| 35 |
:: |
| 36 |
|
| 37 |
Here is my #silent ', '.join(['silly']*5) # example |
| 38 |
|
| 39 |
The output: |
| 40 |
|
| 41 |
:: |
| 42 |
|
| 43 |
Here is my example |
| 44 |
|
| 45 |
The generated code: |
| 46 |
|
| 47 |
:: |
| 48 |
|
| 49 |
write('Here is my ') |
| 50 |
', '.join(['silly']*5) |
| 51 |
write(' example\n') |
| 52 |
|
| 53 |
OK, it's not quite covert because that extra space gives it away, |
| 54 |
but it almost succeeds. |
| 55 |
|
| 56 |
#raw |
| 57 |
---- |
| 58 |
|
| 59 |
|
| 60 |
The template: |
| 61 |
|
| 62 |
:: |
| 63 |
|
| 64 |
Text before raw. |
| 65 |
#raw |
| 66 |
Text in raw. $alligator. $croc.o['dile']. #set $a = $b + $c. |
| 67 |
#end raw |
| 68 |
Text after raw. |
| 69 |
|
| 70 |
The output: |
| 71 |
|
| 72 |
:: |
| 73 |
|
| 74 |
Text before raw. |
| 75 |
Text in raw. $alligator. $croc.o['dile']. #set $a = $b + $c. |
| 76 |
Text after raw. |
| 77 |
|
| 78 |
The generated code: |
| 79 |
|
| 80 |
:: |
| 81 |
|
| 82 |
write('''Text before raw. |
| 83 |
Text in raw. $alligator. $croc.o['dile']. #set $a = $b + $c. |
| 84 |
Text after raw. |
| 85 |
''') |
| 86 |
|
| 87 |
So we see that {#raw} is really like a quoting mechanism. It says |
| 88 |
that anything inside it is ordinary text, and Cheetah joins a |
| 89 |
{#raw} section with adjacent string literals rather than generating |
| 90 |
a separate {write} call. |
| 91 |
|
| 92 |
#include |
| 93 |
-------- |
| 94 |
|
| 95 |
|
| 96 |
The main template: |
| 97 |
|
| 98 |
:: |
| 99 |
|
| 100 |
#include "y.tmpl" |
| 101 |
|
| 102 |
The included template y.tmpl: |
| 103 |
|
| 104 |
:: |
| 105 |
|
| 106 |
Let's go $voom! |
| 107 |
|
| 108 |
The shell command and output: |
| 109 |
|
| 110 |
:: |
| 111 |
|
| 112 |
% voom="VOOM" x.py --env |
| 113 |
Let's go VOOM! |
| 114 |
|
| 115 |
The generated code: |
| 116 |
|
| 117 |
:: |
| 118 |
|
| 119 |
write(self._includeCheetahSource("y.tmpl", trans=trans, includeFrom="file", |
| 120 |
raw=0)) |
| 121 |
|
| 122 |
#include raw |
| 123 |
~~~~~~~~~~~~ |
| 124 |
|
| 125 |
|
| 126 |
The main template: |
| 127 |
|
| 128 |
:: |
| 129 |
|
| 130 |
#include raw "y.tmpl" |
| 131 |
|
| 132 |
The shell command and output: |
| 133 |
|
| 134 |
:: |
| 135 |
|
| 136 |
% voom="VOOM" x.py --env |
| 137 |
Let's go $voom! |
| 138 |
|
| 139 |
The generated code: |
| 140 |
|
| 141 |
:: |
| 142 |
|
| 143 |
write(self._includeCheetahSource("y.tmpl", trans=trans, includeFrom="fil |
| 144 |
e", raw=1)) |
| 145 |
|
| 146 |
That last argument, {raw}, makes the difference. |
| 147 |
|
| 148 |
#include from a string or expression (eval) |
| 149 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 150 |
|
| 151 |
|
| 152 |
The template: |
| 153 |
|
| 154 |
:: |
| 155 |
|
| 156 |
#attr $y = "Let's go $voom!" |
| 157 |
#include source=$y |
| 158 |
#include raw source=$y |
| 159 |
#include source="Bam! Bam!" |
| 160 |
|
| 161 |
The output: |
| 162 |
|
| 163 |
:: |
| 164 |
|
| 165 |
% voom="VOOM" x.py --env |
| 166 |
Let's go VOOM!Let's go $voom!Bam! Bam! |
| 167 |
|
| 168 |
The generated code: |
| 169 |
|
| 170 |
:: |
| 171 |
|
| 172 |
write(self._includeCheetahSource(VFS(SL,"y",1), trans=trans, |
| 173 |
includeFrom="str", raw=0, includeID="481020889808.74")) |
| 174 |
write(self._includeCheetahSource(VFS(SL,"y",1), trans=trans, |
| 175 |
includeFrom="str", raw=1, includeID="711020889808.75")) |
| 176 |
write(self._includeCheetahSource("Bam! Bam!", trans=trans, |
| 177 |
includeFrom="str", raw=0, includeID="1001020889808.75")) |
| 178 |
|
| 179 |
Later in the generated class: |
| 180 |
|
| 181 |
:: |
| 182 |
|
| 183 |
y = "Let's go $voom!" |
| 184 |
|
| 185 |
#slurp |
| 186 |
------ |
| 187 |
|
| 188 |
|
| 189 |
The template: |
| 190 |
|
| 191 |
:: |
| 192 |
|
| 193 |
#for $i in range(5) |
| 194 |
$i |
| 195 |
#end for |
| 196 |
#for $i in range(5) |
| 197 |
$i #slurp |
| 198 |
#end for |
| 199 |
Line after slurp. |
| 200 |
|
| 201 |
The output: |
| 202 |
|
| 203 |
:: |
| 204 |
|
| 205 |
0 |
| 206 |
1 |
| 207 |
2 |
| 208 |
3 |
| 209 |
4 |
| 210 |
0 1 2 3 4 Line after slurp. |
| 211 |
|
| 212 |
The generated code: |
| 213 |
|
| 214 |
:: |
| 215 |
|
| 216 |
for i in range(5): |
| 217 |
write(filter(i)) # generated from '$i' at line 2, col 1. |
| 218 |
write('\n') |
| 219 |
for i in range(5): |
| 220 |
write(filter(i)) # generated from '$i' at line 5, col 1. |
| 221 |
write(' ') |
| 222 |
write('Line after slurp.\n') |
| 223 |
|
| 224 |
The space after each number is because of the space before {#slurp} |
| 225 |
in the template definition. |
| 226 |
|
| 227 |
#filter |
| 228 |
------- |
| 229 |
|
| 230 |
|
| 231 |
The template: |
| 232 |
|
| 233 |
:: |
| 234 |
|
| 235 |
#attr $ode = ">> Rubber Ducky, you're the one! You make bathtime so much fun! <<" |
| 236 |
$ode |
| 237 |
#filter WebSafe |
| 238 |
$ode |
| 239 |
#filter MaxLen |
| 240 |
${ode, maxlen=13} |
| 241 |
#filter None |
| 242 |
${ode, maxlen=13} |
| 243 |
|
| 244 |
The output: |
| 245 |
|
| 246 |
:: |
| 247 |
|
| 248 |
>> Rubber Ducky, you're the one! You make bathtime so much fun! << |
| 249 |
>> Rubber Ducky, you're the one! You make bathtime so much fun! << |
| 250 |
>> Rubber Duc |
| 251 |
>> Rubber Ducky, you're the one! You make bathtime so much fun! << |
| 252 |
|
| 253 |
The {WebSafe} filter escapes characters that have a special meaning |
| 254 |
in HTML. The {MaxLen} filter chops off values at the specified |
| 255 |
length. {#filter None} returns to the default filter, which ignores |
| 256 |
the {maxlen} argument. |
| 257 |
|
| 258 |
The generated code: |
| 259 |
|
| 260 |
:: |
| 261 |
|
| 262 |
1 write(filter(VFS(SL,"ode",1))) # generated from '$ode' at line 2, col 1. |
| 263 |
2 write('\n') |
| 264 |
3 filterName = 'WebSafe' |
| 265 |
4 if "WebSafe" in self._filters: |
| 266 |
5 filter = self._currentFilter = self._filters[filterName] |
| 267 |
6 else: |
| 268 |
7 filter = self._currentFilter = \ |
| 269 |
8 self._filters[filterName] = getattr(self._filtersLib, |
| 270 |
filterName)(self).filter |
| 271 |
9 write(filter(VFS(SL,"ode",1))) # generated from '$ode' at line 4, col 1. |
| 272 |
10 write('\n') |
| 273 |
11 filterName = 'MaxLen' |
| 274 |
12 if "MaxLen" in self._filters: |
| 275 |
13 filter = self._currentFilter = self._filters[filterName] |
| 276 |
14 else: |
| 277 |
15 filter = self._currentFilter = \ |
| 278 |
16 self._filters[filterName] = getattr(self._filtersLib, |
| 279 |
filterName)(self).filter |
| 280 |
17 write(filter(VFS(SL,"ode",1), maxlen=13)) # generated from |
| 281 |
#'${ode, maxlen=13}' at line 6, col 1. |
| 282 |
18 write('\n') |
| 283 |
19 filter = self._initialFilter |
| 284 |
20 write(filter(VFS(SL,"ode",1), maxlen=13)) # generated from |
| 285 |
#'${ode, maxlen=13}' at line 8, col 1. |
| 286 |
21 write('\n') |
| 287 |
|
| 288 |
As we've seen many times, Cheetah wraps all placeholder lookups in |
| 289 |
a {filter} call. (This also applies to non-searchList lookups: |
| 290 |
local, global and builtin variables.) The {filter} "function" is |
| 291 |
actually an alias to the current filter object: |
| 292 |
|
| 293 |
:: |
| 294 |
|
| 295 |
filter = self._currentFilter |
| 296 |
|
| 297 |
as set at the top of the main method. Here in lines 3-8 and 11-16 |
| 298 |
we see the filter being changed. Whoops, I lied. {filter} is not an |
| 299 |
alias to the filter object itself but to that object's {.filter} |
| 300 |
method. Line 19 switches back to the default filter. |
| 301 |
|
| 302 |
In line 17 we see the {maxlen} argument being passed as a keyword |
| 303 |
argument to {filter} (not to {VFS}). In line 20 the same thing |
| 304 |
happens although the default filter ignores the argument. |
| 305 |
|
| 306 |
|