| 1 |
Placeholders |
| 2 |
============ |
| 3 |
|
| 4 |
|
| 5 |
Simple placeholders |
| 6 |
------------------- |
| 7 |
|
| 8 |
|
| 9 |
Let's add a few $placeholders to our template: |
| 10 |
|
| 11 |
:: |
| 12 |
|
| 13 |
>>> from Cheetah.Template import Template |
| 14 |
>>> values = {'what': 'surreal', 'punctuation': '?'} |
| 15 |
>>> t = Template("""\ |
| 16 |
... Hello, $what world$punctuation |
| 17 |
... One of Python's least-used functions is $xrange. |
| 18 |
... """, [values]) |
| 19 |
>>> print t |
| 20 |
Hello, surreal world? |
| 21 |
One of Python's least-used functions is <built-in function xrange>. |
| 22 |
|
| 23 |
>>> print t.generatedModuleCode() |
| 24 |
1 #!/usr/bin/env python |
| 25 |
|
| 26 |
2 """ |
| 27 |
3 Autogenerated by CHEETAH: The Python-Powered Template Engine |
| 28 |
4 CHEETAH VERSION: 0.9.12 |
| 29 |
5 Generation time: Sun Apr 21 00:53:01 2002 |
| 30 |
6 """ |
| 31 |
|
| 32 |
7 __CHEETAH_genTime__ = 'Sun Apr 21 00:53:01 2002' |
| 33 |
8 __CHEETAH_version__ = '0.9.12' |
| 34 |
|
| 35 |
9 ################################################## |
| 36 |
10 ## DEPENDENCIES |
| 37 |
|
| 38 |
11 import sys |
| 39 |
12 import os |
| 40 |
13 import os.path |
| 41 |
14 from os.path import getmtime, exists |
| 42 |
15 import time |
| 43 |
16 import types |
| 44 |
17 from Cheetah.Template import Template |
| 45 |
18 from Cheetah.DummyTransaction import DummyTransaction |
| 46 |
19 from Cheetah.NameMapper import NotFound, valueForName, |
| 47 |
valueFromSearchList |
| 48 |
20 import Cheetah.Filters as Filters |
| 49 |
21 import Cheetah.ErrorCatchers as ErrorCatchers |
| 50 |
|
| 51 |
22 ################################################## |
| 52 |
23 ## MODULE CONSTANTS |
| 53 |
|
| 54 |
24 try: |
| 55 |
25 True, False |
| 56 |
26 except NameError: |
| 57 |
27 True, False = (1==1), (1==0) |
| 58 |
|
| 59 |
28 ################################################## |
| 60 |
29 ## CLASSES |
| 61 |
|
| 62 |
30 class GenTemplate(Template): |
| 63 |
31 """ |
| 64 |
32 |
| 65 |
33 Autogenerated by CHEETAH: The Python-Powered Template Engine |
| 66 |
34 """ |
| 67 |
|
| 68 |
35 ################################################## |
| 69 |
36 ## GENERATED METHODS |
| 70 |
|
| 71 |
|
| 72 |
:: |
| 73 |
|
| 74 |
37 def __init__(self, *args, **KWs): |
| 75 |
38 """ |
| 76 |
39 |
| 77 |
40 """ |
| 78 |
|
| 79 |
41 Template.__init__(self, *args, **KWs) |
| 80 |
|
| 81 |
42 def respond(self, |
| 82 |
43 trans=None, |
| 83 |
44 dummyTrans=False, |
| 84 |
45 VFS=valueFromSearchList, |
| 85 |
46 VFN=valueForName, |
| 86 |
47 getmtime=getmtime, |
| 87 |
48 currentTime=time.time): |
| 88 |
|
| 89 |
|
| 90 |
49 """ |
| 91 |
50 This is the main method generated by Cheetah |
| 92 |
51 """ |
| 93 |
|
| 94 |
52 if not trans: |
| 95 |
53 trans = DummyTransaction() |
| 96 |
54 dummyTrans = True |
| 97 |
55 write = trans.response().write |
| 98 |
56 SL = self._searchList |
| 99 |
57 filter = self._currentFilter |
| 100 |
58 globalSetVars = self._globalSetVars |
| 101 |
59 |
| 102 |
60 ######################################## |
| 103 |
61 ## START - generated method body |
| 104 |
62 |
| 105 |
63 write('Hello, ') |
| 106 |
64 write(filter(VFS(SL,"what",1))) # generated from '$what' at |
| 107 |
# line 1, col 8. |
| 108 |
65 write(' world') |
| 109 |
66 write(filter(VFS(SL,"punctuation",1))) # generated from |
| 110 |
# '$punctuation' at line 1, col 19. |
| 111 |
67 write("\nOne of Python's least-used methods is ") |
| 112 |
68 write(filter(xrange)) # generated from '$xrange' at line 2, |
| 113 |
# col 39. |
| 114 |
69 write('.\n') |
| 115 |
70 |
| 116 |
71 ######################################## |
| 117 |
72 ## END - generated method body |
| 118 |
73 |
| 119 |
74 if dummyTrans: |
| 120 |
75 return trans.response().getvalue() |
| 121 |
76 else: |
| 122 |
77 return "" |
| 123 |
|
| 124 |
:: |
| 125 |
|
| 126 |
78 |
| 127 |
79 ################################################## |
| 128 |
80 ## GENERATED ATTRIBUTES |
| 129 |
|
| 130 |
81 __str__ = respond |
| 131 |
82 _mainCheetahMethod_for_GenTemplate= 'respond' |
| 132 |
|
| 133 |
83 # CHEETAH was developed by Tavis Rudd, Chuck Esterbrook, Ian Bicking |
| 134 |
# and Mike Orr; |
| 135 |
84 # with code, advice and input from many other volunteers. |
| 136 |
85 # For more information visit https://cheetahtemplate.org/ |
| 137 |
|
| 138 |
86 ################################################## |
| 139 |
87 ## if run from command line: |
| 140 |
88 if __name__ == '__main__': |
| 141 |
89 GenTemplate().runAsMainProgram() |
| 142 |
|
| 143 |
|
| 144 |
(Again, I have added line numbers and split the lines as in the |
| 145 |
previous chapter.) |
| 146 |
|
| 147 |
This generated template module is different from the previous one |
| 148 |
in several trivial respects and one important respect. Trivially, |
| 149 |
{.\_filePath} and {.\_fileMtime} are not updated in |
| 150 |
{.\_\_init\_\_}, so they inherit the value {None} from {Template}. |
| 151 |
Also, that if-stanza in {.respond} that recompiles the template if |
| 152 |
the source file changes is missing - because there is no source |
| 153 |
file. So this module is several lines shorter than the other one. |
| 154 |
|
| 155 |
But the important way this module is different is that instead of |
| 156 |
the one {write} call outputting a string literal, this module has a |
| 157 |
series of {write} calls (lines 63-69) outputting successive chunks |
| 158 |
of the template. Regular text has been translated into a string |
| 159 |
literal, and placeholders into function calls. Every placeholder is |
| 160 |
wrapped inside a {filter} call to apply the current output filter. |
| 161 |
(The default output filter converts all objects to strings, and |
| 162 |
{None} to {""}.) |
| 163 |
|
| 164 |
Placeholders referring to a Python builtin like {xrange} (line 68) |
| 165 |
generate a bare variable name. Placeholders to be looked up in the |
| 166 |
searchList have a nested function call; e.g., |
| 167 |
|
| 168 |
:: |
| 169 |
|
| 170 |
write(filter(VFS(SL,"what",1))) # generated from '$what' at line 1, col 8. |
| 171 |
|
| 172 |
{VFS}, remember, is a function imported from {Cheetah.NameMapper} |
| 173 |
that looks up a value in a searchList. So we pass it the |
| 174 |
searchList, the name to look up, and a boolean (1) indicating we |
| 175 |
want autocalling. (It's {1} rather than {True} because it's |
| 176 |
generated from an {and} expression, and that's what Python 2.2 |
| 177 |
outputs for true {and} expressions.) |
| 178 |
|
| 179 |
Complex placeholders |
| 180 |
-------------------- |
| 181 |
|
| 182 |
|
| 183 |
Placeholders can get far more complicated than that. This example |
| 184 |
shows what kind of code the various NameMapper features produce. |
| 185 |
The formulas are taken from Cheetah's test suite, in the |
| 186 |
{Cheetah.Tests.SyntaxAndOutput.Placeholders} class. |
| 187 |
|
| 188 |
:: |
| 189 |
|
| 190 |
1 placeholder: $aStr |
| 191 |
2 placeholders: $aStr $anInt |
| 192 |
2 placeholders, back-to-back: $aStr$anInt |
| 193 |
1 placeholder enclosed in {}: ${aStr} |
| 194 |
1 escaped placeholder: \$var |
| 195 |
func placeholder - with (): $aFunc() |
| 196 |
func placeholder - with (int): $aFunc(1234) |
| 197 |
func placeholder - with (string): $aFunc('aoeu') |
| 198 |
func placeholder - with ('''\nstring'\n'''): $aFunc('''\naoeu'\n''') |
| 199 |
func placeholder - with (string*int): $aFunc('aoeu'*2) |
| 200 |
func placeholder - with (int*float): $aFunc(2*2.0) |
| 201 |
Python builtin values: $None $True $False |
| 202 |
func placeholder - with ($arg=float): $aFunc($arg=4.0) |
| 203 |
deeply nested argstring: $aFunc( $arg = $aMeth( $arg = $aFunc( 1 ) ) ): |
| 204 |
function with None: $aFunc(None) |
| 205 |
autocalling: $aFunc! $aFunc(). |
| 206 |
nested autocalling: $aFunc($aFunc). |
| 207 |
list subscription: $aList[0] |
| 208 |
list slicing: $aList[:2] |
| 209 |
list slicing and subcription combined: $aList[:2][0] |
| 210 |
dict - NameMapper style: $aDict.one |
| 211 |
dict - Python style: $aDict['one'] |
| 212 |
dict combined with autocalled string method: $aDict.one.upper |
| 213 |
dict combined with string method: $aDict.one.upper() |
| 214 |
nested dict - NameMapper style: $aDict.nestedDict.two |
| 215 |
nested dict - Python style: $aDict['nestedDict']['two'] |
| 216 |
nested dict - alternating style: $aDict['nestedDict'].two |
| 217 |
nested dict - NameMapper style + method: $aDict.nestedDict.two.upper |
| 218 |
nested dict - alternating style + method: $aDict['nestedDict'].two.upper |
| 219 |
nested dict - NameMapper style + method + slice: $aDict.nestedDict.two.upper[:4] |
| 220 |
nested dict - Python style, variable key: $aDict[$anObj.meth('nestedDict')].two |
| 221 |
object method: $anObj.meth1 |
| 222 |
object method + complex slice: $anObj.meth1[0: ((4/4*2)*2)/$anObj.meth1(2) ] |
| 223 |
very complex slice: $( anObj.meth1[0: ((4/4*2)*2)/$anObj.meth1(2) ] ) |
| 224 |
$_('a call to gettext') |
| 225 |
|
| 226 |
We'll need a big program to set up the placeholder values. Here it |
| 227 |
is: |
| 228 |
|
| 229 |
:: |
| 230 |
|
| 231 |
#!/usr/bin/env python |
| 232 |
from ComplexExample import ComplexExample |
| 233 |
|
| 234 |
try: # Python >= 2.2.1 |
| 235 |
True, False |
| 236 |
except NameError: # Older Python |
| 237 |
True, False = (1==1), (1==0) |
| 238 |
|
| 239 |
class DummyClass: |
| 240 |
_called = False |
| 241 |
def __str__(self): |
| 242 |
return 'object' |
| 243 |
|
| 244 |
def meth(self, arg="arff"): |
| 245 |
return str(arg) |
| 246 |
|
| 247 |
def meth1(self, arg="doo"): |
| 248 |
return arg |
| 249 |
|
| 250 |
def meth2(self, arg1="a1", arg2="a2"): |
| 251 |
return str(arg1) + str(arg2) |
| 252 |
|
| 253 |
def callIt(self, arg=1234): |
| 254 |
self._called = True |
| 255 |
self._callArg = arg |
| 256 |
|
| 257 |
def dummyFunc(arg="Scooby"): |
| 258 |
return arg |
| 259 |
|
| 260 |
defaultTestNameSpace = { |
| 261 |
'aStr':'blarg', |
| 262 |
'anInt':1, |
| 263 |
'aFloat':1.5, |
| 264 |
'aList': ['item0','item1','item2'], |
| 265 |
'aDict': {'one':'item1', |
| 266 |
'two':'item2', |
| 267 |
'nestedDict':{1:'nestedItem1', |
| 268 |
'two':'nestedItem2' |
| 269 |
}, |
| 270 |
'nestedFunc':dummyFunc, |
| 271 |
}, |
| 272 |
'aFunc': dummyFunc, |
| 273 |
'anObj': DummyClass(), |
| 274 |
'aMeth': DummyClass().meth1, |
| 275 |
'_': lambda x: 'translated ' + x |
| 276 |
} |
| 277 |
|
| 278 |
print ComplexExample( searchList=[defaultTestNameSpace] ) |
| 279 |
|
| 280 |
Here's the output: |
| 281 |
|
| 282 |
:: |
| 283 |
|
| 284 |
1 placeholder: blarg |
| 285 |
2 placeholders: blarg 1 |
| 286 |
2 placeholders, back-to-back: blarg1 |
| 287 |
1 placeholder enclosed in {}: blarg |
| 288 |
1 escaped placeholder: $var |
| 289 |
func placeholder - with (): Scooby |
| 290 |
func placeholder - with (int): 1234 |
| 291 |
func placeholder - with (string): aoeu |
| 292 |
func placeholder - with ('''\nstring'\n'''): |
| 293 |
aoeu' |
| 294 |
|
| 295 |
func placeholder - with (string*int): aoeuaoeu |
| 296 |
func placeholder - with (int*float): 4.0 |
| 297 |
Python builtin values: 1 0 |
| 298 |
func placeholder - with ($arg=float): 4.0 |
| 299 |
deeply nested argstring: 1: |
| 300 |
function with None: |
| 301 |
autocalling: Scooby! Scooby. |
| 302 |
nested autocalling: Scooby. |
| 303 |
list subscription: item0 |
| 304 |
list slicing: ['item0', 'item1'] |
| 305 |
list slicing and subcription combined: item0 |
| 306 |
dict - NameMapper style: item1 |
| 307 |
dict - Python style: item1 |
| 308 |
dict combined with autocalled string method: ITEM1 |
| 309 |
dict combined with string method: ITEM1 |
| 310 |
nested dict - NameMapper style: nestedItem2 |
| 311 |
nested dict - Python style: nestedItem2 |
| 312 |
nested dict - alternating style: nestedItem2 |
| 313 |
nested dict - NameMapper style + method: NESTEDITEM2 |
| 314 |
nested dict - alternating style + method: NESTEDITEM2 |
| 315 |
nested dict - NameMapper style + method + slice: NEST |
| 316 |
nested dict - Python style, variable key: nestedItem2 |
| 317 |
object method: doo |
| 318 |
object method + complex slice: do |
| 319 |
very complex slice: do |
| 320 |
translated a call to gettext |
| 321 |
|
| 322 |
And here - tada! - is the generated module. To save space, I've |
| 323 |
included only the lines containing the {write} calls. The rest of |
| 324 |
the module is the same as in the first example, chapter |
| 325 |
pyModules.example. I've split some of the lines to make them fit on |
| 326 |
the page. |
| 327 |
|
| 328 |
:: |
| 329 |
|
| 330 |
1 write('1 placeholder: ') |
| 331 |
2 write(filter(VFS(SL,"aStr",1))) # generated from '$aStr' at line 1, col 16. |
| 332 |
3 write('\n2 placeholders: ') |
| 333 |
4 write(filter(VFS(SL,"aStr",1))) # generated from '$aStr' at line 2, col 17. |
| 334 |
5 write(' ') |
| 335 |
6 write(filter(VFS(SL,"anInt",1))) |
| 336 |
# generated from '$anInt' at line 2, col 23. |
| 337 |
7 write('\n2 placeholders, back-to-back: ') |
| 338 |
8 write(filter(VFS(SL,"aStr",1))) # generated from '$aStr' at line 3, col 31. |
| 339 |
9 write(filter(VFS(SL,"anInt",1))) |
| 340 |
# generated from '$anInt' at line 3, col 36. |
| 341 |
10 write('\n1 placeholder enclosed in {}: ') |
| 342 |
11 write(filter(VFS(SL,"aStr",1))) # generated from '${aStr}' at line 4, |
| 343 |
# col 31. |
| 344 |
12 write('\n1 escaped placeholder: $var\nfunc placeholder - with (): ') |
| 345 |
13 write(filter(VFS(SL,"aFunc",0)())) # generated from '$aFunc()' at line 6, |
| 346 |
# col 29. |
| 347 |
14 write('\nfunc placeholder - with (int): ') |
| 348 |
15 write(filter(VFS(SL,"aFunc",0)(1234))) # generated from '$aFunc(1234)' at |
| 349 |
# line 7, col 32. |
| 350 |
16 write('\nfunc placeholder - with (string): ') |
| 351 |
17 write(filter(VFS(SL,"aFunc",0)('aoeu'))) # generated from "$aFunc('aoeu')" |
| 352 |
# at line 8, col 35. |
| 353 |
18 write("\nfunc placeholder - with ('''\\nstring'\\n'''): ") |
| 354 |
19 write(filter(VFS(SL,"aFunc",0)('''\naoeu'\n'''))) # generated from |
| 355 |
# "$aFunc('''\\naoeu'\\n''')" at line 9, col 46. |
| 356 |
20 write('\nfunc placeholder - with (string*int): ') |
| 357 |
21 write(filter(VFS(SL,"aFunc",0)('aoeu'*2))) # generated from |
| 358 |
# "$aFunc('aoeu'*2)" at line 10, col 39. |
| 359 |
22 write('\nfunc placeholder - with (int*float): ') |
| 360 |
23 write(filter(VFS(SL,"aFunc",0)(2*2.0))) # generated from '$aFunc(2*2.0)' |
| 361 |
# at line 11, col 38. |
| 362 |
24 write('\nPython builtin values: ') |
| 363 |
25 write(filter(None)) # generated from '$None' at line 12, col 24. |
| 364 |
26 write(' ') |
| 365 |
27 write(filter(True)) # generated from '$True' at line 12, col 30. |
| 366 |
28 write(' ') |
| 367 |
29 write(filter(False)) # generated from '$False' at line 12, col 36. |
| 368 |
30 write('\nfunc placeholder - with ($arg=float): ') |
| 369 |
31 write(filter(VFS(SL,"aFunc",0)(arg=4.0))) # generated from |
| 370 |
# '$aFunc($arg=4.0)' at line 13, col 40. |
| 371 |
32 write('\ndeeply nested argstring: ') |
| 372 |
33 write(filter(VFS(SL,"aFunc",0)( |
| 373 |
arg = VFS(SL,"aMeth",0)( arg = VFS(SL,"aFunc",0)( 1 ) ) ))) |
| 374 |
# generated from '$aFunc( $arg = $aMeth( $arg = $aFunc( 1 ) ) )' |
| 375 |
# at line 14, col 26. |
| 376 |
34 write(':\nfunction with None: ') |
| 377 |
35 write(filter(VFS(SL,"aFunc",0)(None))) # generated from '$aFunc(None)' at |
| 378 |
# line 15, col 21. |
| 379 |
36 write('\nautocalling: ') |
| 380 |
37 write(filter(VFS(SL,"aFunc",1))) # generated from '$aFunc' at line 16, |
| 381 |
# col 14. |
| 382 |
38 write('! ') |
| 383 |
39 write(filter(VFS(SL,"aFunc",0)())) # generated from '$aFunc()' at line 16, |
| 384 |
# col 22. |
| 385 |
|
| 386 |
:: |
| 387 |
|
| 388 |
40 write('.\nnested autocalling: ') |
| 389 |
41 write(filter(VFS(SL,"aFunc",0)(VFS(SL,"aFunc",1)))) # generated from |
| 390 |
# '$aFunc($aFunc)' at line 17, col 21. |
| 391 |
42 write('.\nlist subscription: ') |
| 392 |
43 write(filter(VFS(SL,"aList",1)[0])) # generated from '$aList[0]' at line |
| 393 |
# 18, col 20. |
| 394 |
44 write('\nlist slicing: ') |
| 395 |
45 write(filter(VFS(SL,"aList",1)[:2])) # generated from '$aList[:2]' at |
| 396 |
# line 19, col 15. |
| 397 |
46 write('\nlist slicing and subcription combined: ') |
| 398 |
47 write(filter(VFS(SL,"aList",1)[:2][0])) # generated from '$aList[:2][0]' |
| 399 |
# at line 20, col 40. |
| 400 |
48 write('\ndict - NameMapper style: ') |
| 401 |
49 write(filter(VFS(SL,"aDict.one",1))) # generated from '$aDict.one' at line |
| 402 |
# 21, col 26. |
| 403 |
50 write('\ndict - Python style: ') |
| 404 |
51 write(filter(VFS(SL,"aDict",1)['one'])) # generated from "$aDict['one']" |
| 405 |
# at line 22, col 22. |
| 406 |
52 write('\ndict combined with autocalled string method: ') |
| 407 |
53 write(filter(VFS(SL,"aDict.one.upper",1))) # generated from |
| 408 |
# '$aDict.one.upper' at line 23, col 46. |
| 409 |
54 write('\ndict combined with string method: ') |
| 410 |
55 write(filter(VFN(VFS(SL,"aDict.one",1),"upper",0)())) # generated from |
| 411 |
# '$aDict.one.upper()' at line 24, col 35. |
| 412 |
56 write('\nnested dict - NameMapper style: ') |
| 413 |
57 write(filter(VFS(SL,"aDict.nestedDict.two",1))) # generated from |
| 414 |
# '$aDict.nestedDict.two' at line 25, col 33. |
| 415 |
58 write('\nnested dict - Python style: ') |
| 416 |
59 write(filter(VFS(SL,"aDict",1)['nestedDict']['two'])) # generated from |
| 417 |
# "$aDict['nestedDict']['two']" at line 26, col 29. |
| 418 |
60 write('\nnested dict - alternating style: ') |
| 419 |
61 write(filter(VFN(VFS(SL,"aDict",1)['nestedDict'],"two",1))) # generated |
| 420 |
# from "$aDict['nestedDict'].two" at line 27, col 34. |
| 421 |
62 write('\nnested dict - NameMapper style + method: ') |
| 422 |
63 write(filter(VFS(SL,"aDict.nestedDict.two.upper",1))) # generated from |
| 423 |
# '$aDict.nestedDict.two.upper' at line 28, col 42. |
| 424 |
64 write('\nnested dict - alternating style + method: ') |
| 425 |
65 write(filter(VFN(VFS(SL,"aDict",1)['nestedDict'],"two.upper",1))) |
| 426 |
# generated from "$aDict['nestedDict'].two.upper" at line 29, col 43. |
| 427 |
66 write('\nnested dict - NameMapper style + method + slice: ') |
| 428 |
|
| 429 |
:: |
| 430 |
|
| 431 |
67 write(filter(VFN(VFS(SL,"aDict.nestedDict.two",1),"upper",1)[:4])) |
| 432 |
# generated from '$aDict.nestedDict.two.upper[:4]' at line 30, col 50. |
| 433 |
68 write('\nnested dict - Python style, variable key: ') |
| 434 |
69 write(filter(VFN(VFS(SL,"aDict",1) |
| 435 |
[VFN(VFS(SL,"anObj",1),"meth",0)('nestedDict')],"two",1))) |
| 436 |
# generated from "$aDict[$anObj.meth('nestedDict')].two" at line 31, |
| 437 |
# col 43. |
| 438 |
70 write('\nobject method: ') |
| 439 |
71 write(filter(VFS(SL,"anObj.meth1",1))) # generated from '$anObj.meth1' at |
| 440 |
# line 32, col 16. |
| 441 |
72 write('\nobject method + complex slice: ') |
| 442 |
73 write(filter(VFN(VFS(SL,"anObj",1),"meth1",1) |
| 443 |
[0: ((4/4*2)*2)/VFN(VFS(SL,"anObj",1),"meth1",0)(2) ])) |
| 444 |
# generated from '$anObj.meth1[0: ((4/4*2)*2)/$anObj.meth1(2) ]' |
| 445 |
# at line 33, col 32. |
| 446 |
74 write('\nvery complex slice: ') |
| 447 |
75 write(filter(VFN(VFS(SL,"anObj",1),"meth1",1) |
| 448 |
[0: ((4/4*2)*2)/VFN(VFS(SL,"anObj",1),"meth1",0)(2) ] )) |
| 449 |
# generated from '$( anObj.meth1[0: ((4/4*2)*2)/$anObj.meth1(2) ] )' |
| 450 |
# at line 34, col 21. |
| 451 |
76 if False: |
| 452 |
77 _('foo') |
| 453 |
78 write(filter(VFS(SL,"_",0)("a call to gettext"))) |
| 454 |
# generated from "$_('a call to gettext')" |
| 455 |
# at line 35, col 1. |
| 456 |
79 write('\n') |
| 457 |
|
| 458 |
For each placeholder lookup, the the innermost level of nesting is |
| 459 |
a {VFS} call, which looks up the first (leftmost) placeholder |
| 460 |
component in the searchList. This is wrapped by zero or more {VFN} |
| 461 |
calls, which perform Universal Dotted Notation lookup on the next |
| 462 |
dotted component of the placeholder, looking for an attribute or |
| 463 |
key by that name within the previous object (not in the |
| 464 |
searchList). Autocalling is performed by {VFS} and {VFN}: that's |
| 465 |
the reason for their third argument. |
| 466 |
|
| 467 |
Explicit function/method arguments, subscripts and keys (which are |
| 468 |
all expressions) are left unchanged, besides expanding any embedded |
| 469 |
$placeholders in them. This means they must result in valid Python |
| 470 |
expressions, following the standard Python quoting rules. |
| 471 |
|
| 472 |
Built-in Python values ({None}, {True} and {False}) are converted |
| 473 |
to {filter(None)}, etc. They use normal Python variable lookup |
| 474 |
rather than {VFS}. (Cheetah emulates {True} and {False} using |
| 475 |
global variables for Python < 2.2.1, when they weren't builtins |
| 476 |
yet.) |
| 477 |
|
| 478 |
Notice the last line is a call to {\_} (i.e. {gettext}) which is |
| 479 |
used for internationalization (see |
| 480 |
http://docs.python.org/lib/module-gettext.html). The code is |
| 481 |
converted normally, but an {if False} block is used so that gettext |
| 482 |
can successfully mark the string for translation when parsing the |
| 483 |
generated Python. Otherwise, the NameMapper syntax would get in the |
| 484 |
way of this. |
| 485 |
|
| 486 |
|