| 1 |
.py Template Modules |
| 2 |
==================== |
| 3 |
|
| 4 |
|
| 5 |
This chapter examines the structure of a .py template module. The |
| 6 |
following few chapters will then show how each placeholder and |
| 7 |
directive affects the generated Python code. |
| 8 |
|
| 9 |
An example |
| 10 |
---------- |
| 11 |
|
| 12 |
|
| 13 |
Our first template follows a long noble tradition in computer |
| 14 |
tutorials. It produces a familiar, friendly greeting. Here's the |
| 15 |
template: |
| 16 |
|
| 17 |
:: |
| 18 |
|
| 19 |
Hello, world! |
| 20 |
|
| 21 |
... the output: |
| 22 |
|
| 23 |
:: |
| 24 |
|
| 25 |
Hello, world! |
| 26 |
|
| 27 |
... and the .py template module cheetah-compile produced, with line |
| 28 |
numbers added: |
| 29 |
|
| 30 |
:: |
| 31 |
|
| 32 |
1 #!/usr/bin/env python |
| 33 |
|
| 34 |
2 """ |
| 35 |
3 Autogenerated by CHEETAH: The Python-Powered Template Engine |
| 36 |
4 CHEETAH VERSION: 0.9.12 |
| 37 |
5 Generation time: Sat Apr 20 14:27:47 2002 |
| 38 |
6 Source file: x.tmpl |
| 39 |
7 Source file last modified: Wed Apr 17 22:10:59 2002 |
| 40 |
8 """ |
| 41 |
|
| 42 |
9 __CHEETAH_genTime__ = 'Sat Apr 20 14:27:47 2002' |
| 43 |
10 __CHEETAH_src__ = 'x.tmpl' |
| 44 |
11 __CHEETAH_version__ = '0.9.12' |
| 45 |
|
| 46 |
12 ################################################## |
| 47 |
13 ## DEPENDENCIES |
| 48 |
|
| 49 |
14 import sys |
| 50 |
15 import os |
| 51 |
16 import os.path |
| 52 |
17 from os.path import getmtime, exists |
| 53 |
18 import time |
| 54 |
19 import types |
| 55 |
20 from Cheetah.Template import Template |
| 56 |
21 from Cheetah.DummyTransaction import DummyTransaction |
| 57 |
22 from Cheetah.NameMapper import NotFound, valueForName, |
| 58 |
valueFromSearchList |
| 59 |
23 import Cheetah.Filters as Filters |
| 60 |
24 import Cheetah.ErrorCatchers as ErrorCatchers |
| 61 |
|
| 62 |
25 ################################################## |
| 63 |
26 ## MODULE CONSTANTS |
| 64 |
|
| 65 |
27 try: |
| 66 |
28 True, False |
| 67 |
29 except NameError: |
| 68 |
30 True, False = (1==1), (1==0) |
| 69 |
|
| 70 |
31 ################################################## |
| 71 |
32 ## CLASSES |
| 72 |
|
| 73 |
33 class x(Template): |
| 74 |
34 """ |
| 75 |
35 |
| 76 |
36 Autogenerated by CHEETAH: The Python-Powered Template Engine |
| 77 |
37 """ |
| 78 |
|
| 79 |
:: |
| 80 |
|
| 81 |
38 ################################################## |
| 82 |
39 ## GENERATED METHODS |
| 83 |
|
| 84 |
|
| 85 |
40 def __init__(self, *args, **KWs): |
| 86 |
41 """ |
| 87 |
42 |
| 88 |
43 """ |
| 89 |
|
| 90 |
44 Template.__init__(self, *args, **KWs) |
| 91 |
45 self._filePath = 'x.tmpl' |
| 92 |
46 self._fileMtime = 1019106659 |
| 93 |
|
| 94 |
47 def respond(self, |
| 95 |
48 trans=None, |
| 96 |
49 dummyTrans=False, |
| 97 |
50 VFS=valueFromSearchList, |
| 98 |
51 VFN=valueForName, |
| 99 |
52 getmtime=getmtime, |
| 100 |
53 currentTime=time.time): |
| 101 |
|
| 102 |
|
| 103 |
54 """ |
| 104 |
55 This is the main method generated by Cheetah |
| 105 |
56 """ |
| 106 |
|
| 107 |
57 if not trans: |
| 108 |
58 trans = DummyTransaction() |
| 109 |
59 dummyTrans = True |
| 110 |
60 write = trans.response().write |
| 111 |
61 SL = self._searchList |
| 112 |
62 filter = self._currentFilter |
| 113 |
63 globalSetVars = self._globalSetVars |
| 114 |
64 |
| 115 |
65 ######################################## |
| 116 |
66 ## START - generated method body |
| 117 |
67 |
| 118 |
68 if exists(self._filePath) and getmtime(self._filePath) > \ |
| 119 |
self._fileMtime: |
| 120 |
69 self.compile(file=self._filePath) |
| 121 |
70 write(getattr(self, self._mainCheetahMethod_for_x) |
| 122 |
(trans=trans)) |
| 123 |
71 if dummyTrans: |
| 124 |
72 return trans.response().getvalue() |
| 125 |
73 else: |
| 126 |
74 return "" |
| 127 |
75 write('Hello, world!\n') |
| 128 |
76 |
| 129 |
77 ######################################## |
| 130 |
78 ## END - generated method body |
| 131 |
79 |
| 132 |
80 if dummyTrans: |
| 133 |
81 return trans.response().getvalue() |
| 134 |
82 else: |
| 135 |
83 return "" |
| 136 |
|
| 137 |
:: |
| 138 |
|
| 139 |
84 |
| 140 |
85 ################################################## |
| 141 |
86 ## GENERATED ATTRIBUTES |
| 142 |
|
| 143 |
|
| 144 |
87 __str__ = respond |
| 145 |
|
| 146 |
88 _mainCheetahMethod_for_x= 'respond' |
| 147 |
|
| 148 |
|
| 149 |
89 # CHEETAH was developed by Tavis Rudd, Chuck Esterbrook, Ian Bicking |
| 150 |
# and Mike Orr; |
| 151 |
90 # with code, advice and input from many other volunteers. |
| 152 |
91 # For more information visit https://cheetahtemplate.org/ |
| 153 |
|
| 154 |
92 ################################################## |
| 155 |
93 ## if run from command line: |
| 156 |
94 if __name__ == '__main__': |
| 157 |
95 x().runAsMainProgram() |
| 158 |
|
| 159 |
|
| 160 |
(I added the line numbers for this Guide, and split a few lines to |
| 161 |
fit the page width. The continuation lines don't have line numbers, |
| 162 |
and I added indentation, backslashes and '#' as necessary to make |
| 163 |
the result a valid Python program.) |
| 164 |
|
| 165 |
The examples were generated from CVS versions of Cheetah between |
| 166 |
0.9.12 and 0.9.14. |
| 167 |
|
| 168 |
A walk through the example |
| 169 |
-------------------------- |
| 170 |
|
| 171 |
|
| 172 |
Lines 20-24 are the Cheetah-specific imports. Line 33 introduces |
| 173 |
our generated class, {x}, a subclass of {Template}. It's called x |
| 174 |
because the source file was x.tmpl. |
| 175 |
|
| 176 |
Lines 40-46 are the {.\_\_init\_\_} method called when the template |
| 177 |
is instantiated or used as a Webware servlet, or when the module is |
| 178 |
run as a standalone program. We can see it calling its superclass |
| 179 |
constructor and setting {.\_filePath} and {.\_fileMtime} to the |
| 180 |
filename and modification time (in Unix ticks) of the source .tmpl |
| 181 |
file. |
| 182 |
|
| 183 |
Lines 47-84 are the main method {.respond}, the one that fills the |
| 184 |
template. Normally you call it without arguments, but Webware calls |
| 185 |
it with a Webware {Transaction} object representing the current |
| 186 |
request. Lines 57-59 set up the {trans} variable. If a real or |
| 187 |
dummy transaction is passed in, the method uses it. Otherwise (if |
| 188 |
the {trans} argument is {None}), the method creates a |
| 189 |
{DummyTransaction} instance. {dummyTrans} is a flag that just tells |
| 190 |
whether a dummy transaction is in effect; it'll be used at the end |
| 191 |
of the method. |
| 192 |
|
| 193 |
The other four {.respond} arguments aren't anything you'd ever want |
| 194 |
to pass in; they exist solely to speed up access to these |
| 195 |
frequently-used global functions. This is a standard Python trick |
| 196 |
described in question 4.7 of the Python FAQ |
| 197 |
(http://www.python.org/cgi-bin/faqw.py). {VFS} and {VFN} are the |
| 198 |
functions that give your template the benefits of NameMapper |
| 199 |
lookup, such as the ability to use the searchList. |
| 200 |
|
| 201 |
Line 60 initializes the {write} variable. This important variable |
| 202 |
is discussed below. |
| 203 |
|
| 204 |
Lines 60-63 initialize a few more local variables. {SL} is the |
| 205 |
searchList. {filter} is the current output filter. {globalSetVars} |
| 206 |
are the variables that have been defined with {#set global}. |
| 207 |
|
| 208 |
The comments at lines 65 and 78 delimit the start and end of the |
| 209 |
code that varies with each template. The code outside this region |
| 210 |
is identical in all template modules. That's not quite true - |
| 211 |
{#import} for instance generates additional {import} statements at |
| 212 |
the top of the module - but it's true enough for the most part. |
| 213 |
|
| 214 |
Lines 68-74 exist only if the template source was a named file |
| 215 |
rather than a string or file object. The stanza recompiles the |
| 216 |
template if the source file has changed. Lines 70-74 seem to be |
| 217 |
redundant with 75-83: both fill the template and send the output. |
| 218 |
The reason the first set of lines exists is because the second set |
| 219 |
may become invalid when the template is recompiled. (This is for { |
| 220 |
re} compilation only. The initial compilation happened in the |
| 221 |
{.\_\_init\_\_} method if the template wasn't precompiled.) |
| 222 |
|
| 223 |
Line 75 is the most interesting line in this module. It's a direct |
| 224 |
translation of what we put in the template definition, |
| 225 |
"Hello, world!" Here the content is a single string literal. |
| 226 |
{write} looks like an ordinary function call, but remember that |
| 227 |
line 60 made it an alias to {trans.response().write}, a method in |
| 228 |
the transaction. The next few chapters describe how the different |
| 229 |
placeholders and directives influence this portion of the generated |
| 230 |
class. |
| 231 |
|
| 232 |
Lines 80-83 finish the template filling. If {trans} is a real |
| 233 |
Webware transaction, {write} has already sent the output to Webware |
| 234 |
for handling, so we return {""}. If {trans} is a dummy transaction, |
| 235 |
{write} has been accumulating the output in a Python {StringIO} |
| 236 |
object rather than sending it anywhere, so we have to return it. |
| 237 |
|
| 238 |
Line 83 is the end of the {.respond} method. |
| 239 |
|
| 240 |
Line 87 makes code{.\_\_str\_\_} an alias for the main method, so |
| 241 |
that you can {print} it or apply {str} to it and it will fill the |
| 242 |
template. Line 88 gives the name of the main method, because |
| 243 |
sometimes it's not {.respond}. |
| 244 |
|
| 245 |
Lines 94-95 allow the module to be run directly as a script. |
| 246 |
Essentially, they process the command-line arguments and them make |
| 247 |
the template fill itself. |
| 248 |
|
| 249 |
|