| 1 |
Import, Inheritance, Declaration and Assignment |
| 2 |
=============================================== |
| 3 |
|
| 4 |
|
| 5 |
#import and #from directives |
| 6 |
---------------------------- |
| 7 |
|
| 8 |
|
| 9 |
Syntax: |
| 10 |
|
| 11 |
:: |
| 12 |
|
| 13 |
#import MODULE_OR_OBJECT [as NAME] [, ...] |
| 14 |
#from MODULE import MODULE_OR_OBJECT [as NAME] [, ...] |
| 15 |
|
| 16 |
The {#import} and {#from} directives are used to make external |
| 17 |
Python modules or objects available to placeholders. The syntax is |
| 18 |
identical to the import syntax in Python. Imported modules are |
| 19 |
visible globally to all methods in the generated Python class. |
| 20 |
|
| 21 |
:: |
| 22 |
|
| 23 |
#import math |
| 24 |
#import math as mathModule |
| 25 |
#from math import sin, cos |
| 26 |
#from math import sin as _sin |
| 27 |
#import random, re |
| 28 |
#from mx import DateTime # ## Part of Egenix's mx package. |
| 29 |
|
| 30 |
After the above imports, {$math}, {$mathModule}, {$sin}, {$cos} and |
| 31 |
{$\_sin}, {$random}, {$re} and {$DateTime} may be used in |
| 32 |
{$placeholders} and expressions. |
| 33 |
|
| 34 |
#extends |
| 35 |
-------- |
| 36 |
|
| 37 |
|
| 38 |
Syntax: |
| 39 |
|
| 40 |
:: |
| 41 |
|
| 42 |
#extends CLASS |
| 43 |
|
| 44 |
All templates are subclasses of {Cheetah.Template.Template}. |
| 45 |
However, it's possible for a template to subclass another template |
| 46 |
or a pure Python class. This is where {#extends} steps in: it |
| 47 |
specifies the parent class. It's equivalent to PSP's |
| 48 |
{"@page extends="} directive. |
| 49 |
|
| 50 |
Cheetah imports the class mentioned in an {#extends} directive |
| 51 |
automatically if you haven't imported it yet. The implicit |
| 52 |
importing works like this: |
| 53 |
|
| 54 |
:: |
| 55 |
|
| 56 |
#extends Superclass |
| 57 |
## Implicitly does '#from Superclass import Superclass'. |
| 58 |
|
| 59 |
#extends Cheetah.Templates.SkeletonPage |
| 60 |
## Implicitly does '#from Cheetah.Templates.SkeletonPage import SkeletonPage'. |
| 61 |
|
| 62 |
If your superclass is in an unusual location or in a module named |
| 63 |
differently than the class, you must import it explicitly. There is |
| 64 |
no support for extending from a class that is not imported; e.g., |
| 65 |
from a template dynamically created from a string. Since the most |
| 66 |
practical way to get a parent template into a module is to |
| 67 |
precompile it, all parent templates essentially have to be |
| 68 |
precompiled. |
| 69 |
|
| 70 |
There can be only one {#extends} directive in a template and it may |
| 71 |
list only one class. In other words, templates don't do multiple |
| 72 |
inheritance. This is intentional: it's too hard to initialize |
| 73 |
multiple base classes correctly from inside a template. However, |
| 74 |
you can do multiple inheritance in your pure Python classes. |
| 75 |
|
| 76 |
If your pure Python class overrides any of the standard {Template} |
| 77 |
methods such as {.\_\_init\_\_} or {.awake}, be sure to call the |
| 78 |
superclass method in your method or things will break. Examples of |
| 79 |
calling the superclass method are in section |
| 80 |
tips.callingSuperclassMethods. A list of all superclass methods is |
| 81 |
in section tips.allMethods. |
| 82 |
|
| 83 |
In all cases, the root superclass must be {Template}. If your |
| 84 |
bottommost class is a template, simply omit the {#extends} in it |
| 85 |
and it will automatically inherit from {Template}. { If your |
| 86 |
bottommost class is a pure Python class, it must inherit from |
| 87 |
{Template} explicitly: } |
| 88 |
|
| 89 |
:: |
| 90 |
|
| 91 |
from Cheetah.Template import Template |
| 92 |
class MyPurePythonClass(Template): |
| 93 |
|
| 94 |
If you're not keen about having your Python classes inherit from |
| 95 |
{Template}, create a tiny glue class that inherits both from your |
| 96 |
class and from {Template}. |
| 97 |
|
| 98 |
Before giving any examples we'll stress that Cheetah does { not} |
| 99 |
dictate how you should structure your inheritance tree. As long as |
| 100 |
you follow the rules above, many structures are possible. |
| 101 |
|
| 102 |
Here's an example for a large web site that has not only a general |
| 103 |
site template, but also a template for this section of the site, |
| 104 |
and then a specific template-servlet for each URL. (This is the |
| 105 |
"inheritance approach" discussed in the Webware chapter.) Each |
| 106 |
template inherits from a pure Python class that contains |
| 107 |
methods/attributes used by the template. We'll begin with the |
| 108 |
bottommost superclass and end with the specific template-servlet: |
| 109 |
|
| 110 |
:: |
| 111 |
|
| 112 |
1. SiteLogic.py (pure Python class containing methods for the site) |
| 113 |
from Cheetah.Template import Template |
| 114 |
class SiteLogic(Template): |
| 115 |
|
| 116 |
2. Site.tmpl/py (template containing the general site framework; |
| 117 |
this is the template that controls the output, |
| 118 |
the one that contains "<HTML><HEAD>...", the one |
| 119 |
that contains text outside any #def/#block.) |
| 120 |
#from SiteLogic import SiteLogic |
| 121 |
#extends SiteLogic |
| 122 |
#implements respond |
| 123 |
|
| 124 |
3. SectionLogic.py (pure Python class with helper code for the section) |
| 125 |
from Site import Site |
| 126 |
class SectionLogic(Site) |
| 127 |
|
| 128 |
4. Section.tmpl/py (template with '#def' overrides etc. for the section) |
| 129 |
#from SectionLogic import SectionLogic |
| 130 |
#extends SectionLogic |
| 131 |
|
| 132 |
5. page1Logic.py (pure Python class with helper code for the template-servlet) |
| 133 |
from Section import Section |
| 134 |
class indexLogic(Section): |
| 135 |
|
| 136 |
6. page1.tmpl/py (template-servlet for a certain page on the site) |
| 137 |
#from page1Logic import page1Logic |
| 138 |
#extends page1Logic |
| 139 |
|
| 140 |
A pure Python classes might also contain methods/attributes that |
| 141 |
aren't used by their immediate child template, but are available |
| 142 |
for any descendant template to use if it wishes. For instance, the |
| 143 |
site template might have attributes for the name and e-mail address |
| 144 |
of the site administrator, ready to use as $placeholders in any |
| 145 |
template that wants it. |
| 146 |
|
| 147 |
{ Whenever you use {#extends}, you often need {#implements} too,} |
| 148 |
as in step 2 above. Read the next section to understand what |
| 149 |
{#implements} is and when to use it. |
| 150 |
|
| 151 |
#implements |
| 152 |
----------- |
| 153 |
|
| 154 |
|
| 155 |
Syntax: |
| 156 |
|
| 157 |
:: |
| 158 |
|
| 159 |
#implements METHOD |
| 160 |
|
| 161 |
You can call any {#def} or {#block} method directly and get its |
| 162 |
outpt. The top-level content - all the text/placeholders/directives |
| 163 |
outside any {#def}/{#block} - gets concatenated and wrapped in a |
| 164 |
"main method", by default {.respond()}. So if you call |
| 165 |
{.respond()}, you get the "whole template output". When Webware |
| 166 |
calls {.respond()}, that's what it's doing. And when you do 'print |
| 167 |
t' or 'str(t)' on a template instance, you're taking advantage of |
| 168 |
the fact that Cheetah makes {.\_\_str\_\_()} an alias for the main |
| 169 |
method. |
| 170 |
|
| 171 |
That's all fine and dandy, but what if your application prefers to |
| 172 |
call another method name rather than {.respond()}? What if it wants |
| 173 |
to call, say, {.send\_output()} instead? That's where {#implements} |
| 174 |
steps in. It lets you choose the name for the main method. Just put |
| 175 |
this in your template definition: |
| 176 |
|
| 177 |
:: |
| 178 |
|
| 179 |
#implements send_output |
| 180 |
|
| 181 |
When one template extends another, every template in the |
| 182 |
inheritance chain has its own main method. To fill the template, |
| 183 |
you invoke exactly one of these methods and the others are ignored. |
| 184 |
The method you call may be in any of the templates in the |
| 185 |
inheritance chain: the base template, the leaf template, or any in |
| 186 |
between, depending on how you structure your application. So you |
| 187 |
have two problems: (1) calling the right method name, and (2) |
| 188 |
preventing an undesired same-name subclass method from overriding |
| 189 |
the one you want to call. |
| 190 |
|
| 191 |
Cheetah assumes the method you will call is {.respond()} because |
| 192 |
that's what Webware calls. It further assumes the desired main |
| 193 |
method is the one in the lowest-level base template, because that |
| 194 |
works well with {#block} as described in the Inheritance Approach |
| 195 |
for building Webware servlets (section webware.inheritance), which |
| 196 |
was originally the principal use for Cheetah. So when you use |
| 197 |
{#extends}, Cheetah changes that template's main method to |
| 198 |
{.writeBody()} to get it out of the way and prevent it from |
| 199 |
overriding the base template's {.respond()}. |
| 200 |
|
| 201 |
Unfortunately this assumption breaks down if the template is used |
| 202 |
in other ways. For instance, you may want to use the main method in |
| 203 |
the highest-level leaf template, and treat the base template(s) as |
| 204 |
merely a library of methods/attributes. In that case, the leaf |
| 205 |
template needs {#implements respond} to change its main method name |
| 206 |
back to {.respond()} (or whatever your application desires to |
| 207 |
call). Likewise, if your main method is in one of the intermediate |
| 208 |
templates in an inheritance chain, that template needs {#implements |
| 209 |
respond}. |
| 210 |
|
| 211 |
The other way the assumption breaks down is if the main method { |
| 212 |
is} in the base template but that template extends a pure Python |
| 213 |
class. Cheetah sees the {#extends} and dutifully but incorrectly |
| 214 |
renames the method to {.writeBody()}, so you have to use |
| 215 |
{#implements respond} to change it back. Otherwise the dummy |
| 216 |
{.respond()} in {Cheetah.Template} is found, which outputs... |
| 217 |
nothing. { So if you're using {#extends} and get no output, the { |
| 218 |
first} thing you should think is, |
| 219 |
"Do I need to add {#implements respond} somewhere?" } |
| 220 |
|
| 221 |
#set |
| 222 |
---- |
| 223 |
|
| 224 |
|
| 225 |
Syntax: |
| 226 |
|
| 227 |
:: |
| 228 |
|
| 229 |
#set [global] $var = EXPR |
| 230 |
|
| 231 |
{#set} is used to create and update local variables at run time. |
| 232 |
The expression may be any Python expression. Remember to preface |
| 233 |
variable names with $ unless they're part of an intermediate result |
| 234 |
in a list comprehension. |
| 235 |
|
| 236 |
Here are some examples: |
| 237 |
|
| 238 |
:: |
| 239 |
|
| 240 |
#set $size = $length * 1096 |
| 241 |
#set $buffer = $size + 1096 |
| 242 |
#set $area = $length * $width |
| 243 |
#set $namesList = ['Moe','Larry','Curly'] |
| 244 |
#set $prettyCountry = $country.replace(' ', ' ') |
| 245 |
|
| 246 |
{#set} variables are useful to assign a short name to a |
| 247 |
{$deeply.nested.value}, to a calculation, or to a printable version |
| 248 |
of a value. The last example above converts any spaces in the |
| 249 |
'country' value into HTML non-breakable-space entities, to ensure |
| 250 |
the entire value appears on one line in the browser. |
| 251 |
|
| 252 |
{#set} variables are also useful in {#if} expressions, but remember |
| 253 |
that complex logical routines should be coded in Python, not in |
| 254 |
Cheetah! |
| 255 |
|
| 256 |
:: |
| 257 |
|
| 258 |
#if $size > 1500 |
| 259 |
#set $adj = 'large' |
| 260 |
#else |
| 261 |
#set $adj = 'small' |
| 262 |
#end if |
| 263 |
|
| 264 |
Or Python's one-line equivalent, "A and B or C". Remember that in |
| 265 |
this case, B must be a true value (not None, '', 0, [] or {}). |
| 266 |
|
| 267 |
:: |
| 268 |
|
| 269 |
#set $adj = $size > 1500 and 'large' or 'small' |
| 270 |
|
| 271 |
(Note: Cheetah's one-line {#if} will not work for this, since it |
| 272 |
produces output rather than setting a variable. |
| 273 |
|
| 274 |
You can also use the augmented assignment operators: |
| 275 |
|
| 276 |
:: |
| 277 |
|
| 278 |
## Increment $a by 5. |
| 279 |
#set $a += 5 |
| 280 |
|
| 281 |
By default, {#set} variables are not visible in method calls or |
| 282 |
include files unless you use the {global} attribute: {#set global |
| 283 |
$var = EXPRESSION}. Global variables are visible in all methods, |
| 284 |
nested templates and included files. Use this feature with care to |
| 285 |
prevent surprises. |
| 286 |
|
| 287 |
#del |
| 288 |
---- |
| 289 |
|
| 290 |
|
| 291 |
Syntax: |
| 292 |
|
| 293 |
:: |
| 294 |
|
| 295 |
#del $var |
| 296 |
|
| 297 |
{#del} is the opposite of {#set}. It deletes a { local} variable. |
| 298 |
Its usage is just like Python's {del} statement: |
| 299 |
|
| 300 |
:: |
| 301 |
|
| 302 |
#del $myVar |
| 303 |
#del $myVar, $myArray[5] |
| 304 |
|
| 305 |
Only local variables can be deleted. There is no directive to |
| 306 |
delete a {#set global} variable, a searchList variable, or any |
| 307 |
other type of variable. |
| 308 |
|
| 309 |
#attr |
| 310 |
----- |
| 311 |
|
| 312 |
|
| 313 |
Syntax: |
| 314 |
|
| 315 |
:: |
| 316 |
|
| 317 |
#attr $var = EXPR |
| 318 |
|
| 319 |
The {#attr} directive creates class attributes in the generated |
| 320 |
Python class. It should be used to assign simple Python literals |
| 321 |
such as numbers or strings. In particular, the expression must { |
| 322 |
not} depend on searchList values or {#set} variables since those |
| 323 |
are not known at compile time. |
| 324 |
|
| 325 |
:: |
| 326 |
|
| 327 |
#attr $title = "Rob Roy" |
| 328 |
#attr $author = "Sir Walter Scott" |
| 329 |
#attr $version = 123.4 |
| 330 |
|
| 331 |
This template or any child template can output the value thus: |
| 332 |
|
| 333 |
:: |
| 334 |
|
| 335 |
$title, by $author, version $version |
| 336 |
|
| 337 |
If you have a library of templates derived from etexts |
| 338 |
(http://www.gutenberg.org/), you can extract the titles and authors |
| 339 |
and put them in a database (assuming the templates have been |
| 340 |
compiled into .py template modules): |
| 341 |
|
| 342 |
#def |
| 343 |
---- |
| 344 |
|
| 345 |
|
| 346 |
Syntax: |
| 347 |
|
| 348 |
:: |
| 349 |
|
| 350 |
#def METHOD[(ARGUMENTS)] |
| 351 |
#end def |
| 352 |
|
| 353 |
Or the one-line variation: |
| 354 |
|
| 355 |
:: |
| 356 |
|
| 357 |
#def METHOD[(ARGUMENTS)] : TEXT_AND_PLACEHOLDERS |
| 358 |
|
| 359 |
The {#def} directive is used to define new methods in the generated |
| 360 |
Python class, or to override superclass methods. It is analogous to |
| 361 |
Python's {def} statement. The directive is silent, meaning it does |
| 362 |
not itself produce any output. However, the content of the method |
| 363 |
will be inserted into the output (and the directives executed) |
| 364 |
whenever the method is later called by a $placeholder. |
| 365 |
|
| 366 |
:: |
| 367 |
|
| 368 |
#def myMeth() |
| 369 |
This is the text in my method |
| 370 |
$a $b $c(123) ## these placeholder names have been defined elsewhere |
| 371 |
#end def |
| 372 |
|
| 373 |
## and now use it... |
| 374 |
$myMeth() |
| 375 |
|
| 376 |
The arglist and parentheses can be omitted: |
| 377 |
|
| 378 |
:: |
| 379 |
|
| 380 |
#def myMeth |
| 381 |
This is the text in my method |
| 382 |
$a $b $c(123) |
| 383 |
#end def |
| 384 |
|
| 385 |
## and now use it... |
| 386 |
$myMeth |
| 387 |
|
| 388 |
Methods can have arguments and have defaults for those arguments, |
| 389 |
just like in Python. Remember the {$} before variable names: |
| 390 |
|
| 391 |
:: |
| 392 |
|
| 393 |
#def myMeth($a, $b=1234) |
| 394 |
This is the text in my method |
| 395 |
$a - $b |
| 396 |
#end def |
| 397 |
|
| 398 |
## and now use it... |
| 399 |
$myMeth(1) |
| 400 |
|
| 401 |
The output from this last example will be: |
| 402 |
|
| 403 |
:: |
| 404 |
|
| 405 |
This is the text in my method |
| 406 |
1 - 1234 |
| 407 |
|
| 408 |
There is also a single line version of the {#def} directive. { |
| 409 |
Unlike the multi-line directives, it uses a colon (:) to delimit |
| 410 |
the method signature and body}: |
| 411 |
|
| 412 |
:: |
| 413 |
|
| 414 |
#attr $adj = 'trivial' |
| 415 |
#def myMeth: This is the $adj method |
| 416 |
$myMeth |
| 417 |
|
| 418 |
Leading and trailing whitespace is stripped from the method. This |
| 419 |
is in contrast to: |
| 420 |
|
| 421 |
:: |
| 422 |
|
| 423 |
#def myMeth2 |
| 424 |
This is the $adj method |
| 425 |
#end def |
| 426 |
|
| 427 |
where the method includes a newline after "method". If you don't |
| 428 |
want the newline, add {#slurp}: |
| 429 |
|
| 430 |
:: |
| 431 |
|
| 432 |
#def myMeth3 |
| 433 |
This is the $adj method#slurp |
| 434 |
#end def |
| 435 |
|
| 436 |
Because {#def} is handled at compile time, it can appear above or |
| 437 |
below the placeholders that call it. And if a superclass |
| 438 |
placeholder calls a method that's overridden in a subclass, it's |
| 439 |
the subclass method that will be called. |
| 440 |
|
| 441 |
#block ... #end block |
| 442 |
--------------------- |
| 443 |
|
| 444 |
|
| 445 |
The {#block} directive allows you to mark a section of your |
| 446 |
template that can be selectively reimplemented in a subclass. It is |
| 447 |
very useful for changing part of a template without having to |
| 448 |
copy-paste-and-edit the entire thing. The output from a template |
| 449 |
definition that uses blocks will be identical to the output from |
| 450 |
the same template with the {#block ... #end block} tags removed. |
| 451 |
|
| 452 |
({ Note:} don't be confused by the generic word 'block'' in this |
| 453 |
Guide, which means a section of code inside { any} {#TAG ... #end |
| 454 |
TAG} pair. Thus, an if-block, for-block, def-block, block-block |
| 455 |
etc. In this section we are talking only of block-blocks.) |
| 456 |
|
| 457 |
To reimplement the block, use the {#def} directive. The magical |
| 458 |
effect is that it appears to go back and change the output text { |
| 459 |
at the point the original block was defined} rather than at the |
| 460 |
location of the reimplementation. |
| 461 |
|
| 462 |
:: |
| 463 |
|
| 464 |
#block testBlock |
| 465 |
Text in the contents |
| 466 |
area of the block directive |
| 467 |
#if $testIt |
| 468 |
$getFoo() |
| 469 |
#end if |
| 470 |
#end block testBlock |
| 471 |
|
| 472 |
You can repeat the block name in the {#end block} directive or not, |
| 473 |
as you wish. |
| 474 |
|
| 475 |
{#block} directives can be nested to any depth. |
| 476 |
|
| 477 |
:: |
| 478 |
|
| 479 |
#block outerBlock |
| 480 |
Outer block contents |
| 481 |
|
| 482 |
#block innerBlock1 |
| 483 |
inner block1 contents |
| 484 |
#end block innerBlock1 |
| 485 |
|
| 486 |
#block innerBlock2 |
| 487 |
inner block2 contents |
| 488 |
#end block innerBlock2 |
| 489 |
|
| 490 |
#end block outerBlock |
| 491 |
|
| 492 |
Note that the name of the block is optional for the {#end block} |
| 493 |
tag. |
| 494 |
|
| 495 |
Technically, {#block} directive is equivalent to a {#def} directive |
| 496 |
followed immediately by a {#placeholder} for the same name. In |
| 497 |
fact, that's what Cheetah does. Which means you can use |
| 498 |
{$theBlockName} elsewhere in the template to output the block |
| 499 |
content again. |
| 500 |
|
| 501 |
There is a one-line {#block} syntax analagous to the one-line |
| 502 |
{#def}. |
| 503 |
|
| 504 |
The block must not require arguments because the implicit |
| 505 |
placeholder that's generated will call the block without |
| 506 |
arguments. |
| 507 |
|
| 508 |
|