| 1 |
.. role:: math(raw) |
| 2 |
:format: html latex |
| 3 |
|
| 4 |
Language Overview |
| 5 |
================= |
| 6 |
|
| 7 |
|
| 8 |
Cheetah's basic syntax was inspired by the Java-based template |
| 9 |
engines Velocity and WebMacro. It has two types of tags: { |
| 10 |
$placeholders} and { #directives}. Both types are case-sensitive. |
| 11 |
|
| 12 |
Placeholder tags begin with a dollar sign ({$varName}) and are |
| 13 |
similar to data fields in a form letter or to the {%(key)s} fields |
| 14 |
on the left side of Python's {%} operator. When the template is |
| 15 |
filled, the placeholders are replaced with the values they refer |
| 16 |
to. |
| 17 |
|
| 18 |
Directive tags begin with a hash character (#) and are used for |
| 19 |
comments, loops, conditional blocks, includes, and all other |
| 20 |
advanced features. ({ Note:} you can customize the start and end |
| 21 |
delimeters for placeholder and directive tags, but in this Guide |
| 22 |
we'll assume you're using the default.) |
| 23 |
|
| 24 |
Placeholders and directives can be escaped by putting a backslash |
| 25 |
before them. ``\$var`` and ``\#if`` will be output as literal |
| 26 |
text. |
| 27 |
|
| 28 |
A placeholder or directive can span multiple physical lines, |
| 29 |
following the same rules as Python source code: put a backslash |
| 30 |
(``\``) at the end of all lines except the last line. However, if |
| 31 |
there's an unclosed parenthesis, bracket or brace pending, you |
| 32 |
don't need the backslash. |
| 33 |
|
| 34 |
:: |
| 35 |
|
| 36 |
#if $this_is_a_very_long_line and $has_lots_of_conditions \ |
| 37 |
and $more_conditions: |
| 38 |
<H1>bla</H1> |
| 39 |
#end if |
| 40 |
|
| 41 |
#if $country in ('Argentina', 'Uruguay', 'Peru', 'Colombia', |
| 42 |
'Costa Rica', 'Venezuela', 'Mexico') |
| 43 |
<H1>Hola, senorita!</H1> |
| 44 |
#else |
| 45 |
<H1>Hey, baby!</H1> |
| 46 |
#end if |
| 47 |
|
| 48 |
Language Constructs - Summary |
| 49 |
----------------------------- |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
#. Comments and documentation strings |
| 54 |
|
| 55 |
|
| 56 |
#. {## single line} |
| 57 |
|
| 58 |
#. {#\* multi line \*#} |
| 59 |
|
| 60 |
|
| 61 |
#. Generation, caching and filtering of output |
| 62 |
|
| 63 |
|
| 64 |
#. plain text |
| 65 |
|
| 66 |
#. look up a value: {$placeholder} |
| 67 |
|
| 68 |
#. evaluate an expression: {#echo} ... |
| 69 |
|
| 70 |
#. same but discard the output: {#silent} ... |
| 71 |
|
| 72 |
#. one-line if: {#if EXPR then EXPR else EXPR} |
| 73 |
|
| 74 |
#. gobble the EOL: {#slurp} |
| 75 |
|
| 76 |
#. parsed file includes: {#include} ... |
| 77 |
|
| 78 |
#. raw file includes: {#include raw} ... |
| 79 |
|
| 80 |
#. verbatim output of Cheetah code: {#raw} ... {#end raw} |
| 81 |
|
| 82 |
#. cached placeholders: {$\*var}, {$\*<interval>\*var} |
| 83 |
|
| 84 |
#. cached regions: {#cache} ... {#end cache} |
| 85 |
|
| 86 |
#. set the output filter: {#filter} ... |
| 87 |
|
| 88 |
#. control output indentation: {#indent} ... ({ not implemented |
| 89 |
yet}) |
| 90 |
|
| 91 |
|
| 92 |
#. Importing Python modules and objects: {#import} ..., {#from} |
| 93 |
... |
| 94 |
|
| 95 |
#. Inheritance |
| 96 |
|
| 97 |
|
| 98 |
#. set the base class to inherit from: {#extends} |
| 99 |
|
| 100 |
#. set the name of the main method to implement: {#implements} |
| 101 |
... |
| 102 |
|
| 103 |
|
| 104 |
#. Compile-time declaration |
| 105 |
|
| 106 |
|
| 107 |
#. define class attributes: {#attr} ... |
| 108 |
|
| 109 |
#. define class methods: {#def} ... {#end def} |
| 110 |
|
| 111 |
#. {#block} ... {#end block} provides a simplified interface to |
| 112 |
{#def} ... {#end def} |
| 113 |
|
| 114 |
|
| 115 |
#. Run-time assignment |
| 116 |
|
| 117 |
|
| 118 |
#. local vars: {#set} ... |
| 119 |
|
| 120 |
#. global vars: {#set global} ... |
| 121 |
|
| 122 |
#. deleting local vars: {#del} ... |
| 123 |
|
| 124 |
|
| 125 |
#. Flow control |
| 126 |
|
| 127 |
|
| 128 |
#. {#if} ... {#else} ... {#else if} (aka {#elif}) ... {#end if} |
| 129 |
|
| 130 |
#. {#unless} ... {#end unless} |
| 131 |
|
| 132 |
#. {#for} ... {#end for} |
| 133 |
|
| 134 |
#. {#repeat} ... {#end repeat} |
| 135 |
|
| 136 |
#. {#while} ... {#end while} |
| 137 |
|
| 138 |
#. {#break} |
| 139 |
|
| 140 |
#. {#continue} |
| 141 |
|
| 142 |
#. {#pass} |
| 143 |
|
| 144 |
#. {#stop} |
| 145 |
|
| 146 |
|
| 147 |
#. error/exception handling |
| 148 |
|
| 149 |
|
| 150 |
#. {#assert} |
| 151 |
|
| 152 |
#. {#raise} |
| 153 |
|
| 154 |
#. {#try} ... {#except} ... {#else} ... {#end try} |
| 155 |
|
| 156 |
#. {#try} ... {#finally} ... {#end try} |
| 157 |
|
| 158 |
#. {#errorCatcher} ... set a handler for exceptions raised by |
| 159 |
$placeholder calls. |
| 160 |
|
| 161 |
|
| 162 |
#. Instructions to the parser/compiler |
| 163 |
|
| 164 |
|
| 165 |
#. {#breakpoint} |
| 166 |
|
| 167 |
#. {#compiler-settings} ... {#end compiler-settings} |
| 168 |
|
| 169 |
|
| 170 |
#. Escape to pure Python code |
| 171 |
|
| 172 |
|
| 173 |
#. evalute expression and print the output: {<%=} ... {%>} |
| 174 |
|
| 175 |
#. execute code and discard output: {<%} ... {%>} |
| 176 |
|
| 177 |
|
| 178 |
#. Fine control over Cheetah-generated Python modules |
| 179 |
|
| 180 |
|
| 181 |
#. set the source code encoding of compiled template modules: |
| 182 |
{#encoding} |
| 183 |
|
| 184 |
#. set the sh-bang line of compiled template modules: {#shBang} |
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
The use of all these constructs will be covered in the next several |
| 189 |
chapters. |
| 190 |
|
| 191 |
Placeholder Syntax Rules |
| 192 |
------------------------ |
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
- Placeholders follow the same syntax rules as Python variables |
| 197 |
except that they are preceded by {$} (the short form) or enclosed |
| 198 |
in {${}} (the long form). Examples: |
| 199 |
|
| 200 |
:: |
| 201 |
|
| 202 |
$var |
| 203 |
${var} |
| 204 |
$var2.abc['def']('gh', $subplaceholder, 2) |
| 205 |
${var2.abc['def']('gh', $subplaceholder, 2)} |
| 206 |
|
| 207 |
We recommend {$} in simple cases, and {${}} when followed directly |
| 208 |
by a letter or when Cheetah or a human template maintainer might |
| 209 |
get confused about where the placeholder ends. You may alternately |
| 210 |
use ``$()`` or ``$[]``, although this may confuse the (human) |
| 211 |
template maintainer: |
| 212 |
|
| 213 |
:: |
| 214 |
|
| 215 |
$(var) |
| 216 |
$[var] |
| 217 |
$(var2.abc['def']('gh', $subplaceholder, 2)) |
| 218 |
$[var2.abc['def']('gh', $subplaceholder, 2)] |
| 219 |
|
| 220 |
{ Note:} Advanced users can change the delimiters to anything they |
| 221 |
want via the {#compiler} directive. |
| 222 |
|
| 223 |
{ Note 2:} The long form can be used only with top-level |
| 224 |
placeholders, not in expressions. See section |
| 225 |
language.placeholders.positions for an elaboration on this. |
| 226 |
|
| 227 |
- To reiterate Python's rules, placeholders consist of one or more |
| 228 |
identifiers separated by periods. Each identifier must start with a |
| 229 |
letter or an underscore, and the subsequent characters must be |
| 230 |
letters, digits or underscores. Any identifier may be followed by |
| 231 |
arguments enclosed in ``()`` and/or keys/subscripts in ``[]``. |
| 232 |
|
| 233 |
- Identifiers are case sensitive. {$var} does not equal {$Var} or |
| 234 |
{$vAr} or {$VAR}. |
| 235 |
|
| 236 |
- Arguments inside ``()`` or ``[]`` are just like in Python. |
| 237 |
Strings may be quoted using any Python quoting style. Each argument |
| 238 |
is an expression and may use any of Python's expression operators. |
| 239 |
Variables used in argument expressions are placeholders and should |
| 240 |
be prefixed with {$}. This also applies to the \*arg and \*\*kw |
| 241 |
forms. However, you do { not} need the {$} with the special Python |
| 242 |
constants {None}, {True} and {False}. Examples: |
| 243 |
|
| 244 |
:: |
| 245 |
|
| 246 |
$hex($myVar) |
| 247 |
$func($arg=1234) |
| 248 |
$func2($*args, $**kw) |
| 249 |
$func3(3.14159, $arg2, None, True) |
| 250 |
$myList[$mySubscript] |
| 251 |
|
| 252 |
- Trailing periods are ignored. Cheetah will recognize that the |
| 253 |
placeholder name in {$varName.} is {varName}, and the period will |
| 254 |
be left alone in the template output. |
| 255 |
|
| 256 |
- The syntax {${placeholderName, arg1="val1"}} passes arguments to |
| 257 |
the output filter (see {#filter}, section output.filter. The braces |
| 258 |
and comma are required in this case. It's conventional to omit the |
| 259 |
{$} before the keyword arguments (i.e. {arg1}) in this case. |
| 260 |
|
| 261 |
- Cheetah ignores all dollar signs ({$}) that are not followed by |
| 262 |
a letter or an underscore. |
| 263 |
|
| 264 |
|
| 265 |
The following are valid $placeholders: |
| 266 |
|
| 267 |
:: |
| 268 |
|
| 269 |
$a $_ $var $_var $var1 $_1var $var2_ $dict.key $list[3] |
| 270 |
$object.method $object.method() $object.method |
| 271 |
$nest($nest($var)) |
| 272 |
|
| 273 |
These are not $placeholders but are treated as literal text: |
| 274 |
|
| 275 |
:: |
| 276 |
|
| 277 |
$@var $^var $15.50 $$ |
| 278 |
|
| 279 |
Where can you use placeholders? |
| 280 |
------------------------------- |
| 281 |
|
| 282 |
|
| 283 |
There are three places you can use placeholders: top-level |
| 284 |
position, expression position and LVALUE position. Each has |
| 285 |
slightly different syntax rules. |
| 286 |
|
| 287 |
Top-level position means interspersed in text. This is the only |
| 288 |
place you can use the placeholder long form: {${var}}. |
| 289 |
|
| 290 |
{ Expression position} means inside a Cheetah expression, which is |
| 291 |
the same as a Python expression. The placeholder names a searchList |
| 292 |
or other variable to be read. Expression position occurs inside () |
| 293 |
and :math:`$[]$` arguments within placeholder tags (i.e., a |
| 294 |
placeholder inside a placeholder), and in several directive tags. |
| 295 |
|
| 296 |
{ LVALUE position} means naming a variable that will be written to. |
| 297 |
LVALUE is a computer science term meaning |
| 298 |
"the left side of an assignment statement". The first argument of |
| 299 |
directives {#set}, {#for}, {#def}, {#block} and {#attr} is an |
| 300 |
LVALUE. |
| 301 |
|
| 302 |
This stupid example shows the three positions. Top-level position |
| 303 |
is shown in {courier}, expression position is { italic}, and LVALUE |
| 304 |
position is { bold}. |
| 305 |
|
| 306 |
#for { $count} in { $range}({ $ninetyNine}, 0, -1) |
| 307 |
#set { $after} = { $count} - 1 |
| 308 |
{$count} bottles of beer on the wall. {$count} bottles of beer! |
| 309 |
Take one down, pass it around. {$after} bottles of beer on the |
| 310 |
wall. |
| 311 |
#end for |
| 312 |
{$hex}({ $myVar}, { $default}={ None}) |
| 313 |
|
| 314 |
|
| 315 |
The output of course is: |
| 316 |
|
| 317 |
:: |
| 318 |
|
| 319 |
99 bottles of beer on the wall. 99 bottles of beer! |
| 320 |
Take one down, pass it around. 98 bottles of beer on the wall. |
| 321 |
98 bottles of beer on the wall. 98 bottles of beer! |
| 322 |
Take one down, pass it around. 97 bottles of beer on the wall. |
| 323 |
... |
| 324 |
|
| 325 |
Are all those dollar signs really necessary? |
| 326 |
-------------------------------------------- |
| 327 |
|
| 328 |
|
| 329 |
{$} is a "smart variable prefix". When Cheetah sees {$}, it |
| 330 |
determines both the variable's position and whether it's a |
| 331 |
searchList value or a non-searchList value, and generates the |
| 332 |
appropriate Python code. |
| 333 |
|
| 334 |
In top-level position, the {$} is { required}. Otherwise there's |
| 335 |
nothing to distinguish the variable from ordinary text, and the |
| 336 |
variable name is output verbatim. |
| 337 |
|
| 338 |
In expression position, the {$} is { required} if the value comes |
| 339 |
from the searchList or a "#set global" variable, { recommended} for |
| 340 |
local/global/builtin variables, and { not necessary} for the |
| 341 |
special constants {None}, {True} and {False}. This works because |
| 342 |
Cheetah generates a function call for a searchList placeholder, but |
| 343 |
a bare variable name for a local/global/builtin variable. |
| 344 |
|
| 345 |
In LVALUE position, the {$} is { recommended}. Cheetah knows where |
| 346 |
an LVALUE is expected, so it can handle your variable name whether |
| 347 |
it has {$} or not. |
| 348 |
|
| 349 |
EXCEPTION: Do { not} use the {$} prefix for intermediate variables |
| 350 |
in a Python list comprehensions. This is a limitation of Cheetah's |
| 351 |
parser; it can't tell which variables in a list comprehension are |
| 352 |
the intermediate variables, so you have to help it. For example: |
| 353 |
|
| 354 |
:: |
| 355 |
|
| 356 |
#set $theRange = [x ** 2 for x in $range(10)] |
| 357 |
|
| 358 |
{$theRange} is a regular {#set} variable. {$range} is a Python |
| 359 |
built-in function. But {x} is a scratch variable internal to the |
| 360 |
list comprehension: if you type {$x}, Cheetah will miscompile it. |
| 361 |
|
| 362 |
NameMapper Syntax |
| 363 |
----------------- |
| 364 |
|
| 365 |
|
| 366 |
One of our core aims for Cheetah was to make it easy for |
| 367 |
non-programmers to use. Therefore, Cheetah uses a simplified syntax |
| 368 |
for mapping placeholders in Cheetah to values in Python. It's known |
| 369 |
as the { NameMapper syntax} and allows for non-programmers to use |
| 370 |
Cheetah without knowing (a) the difference between an instance and |
| 371 |
a dictionary, (b) what functions and methods are, and (c) what |
| 372 |
'self' is. A side benefit is that you can change the underlying |
| 373 |
data structure (e.g., instance to dictionary or vice-versa) without |
| 374 |
having to modify the templates. |
| 375 |
|
| 376 |
NameMapper syntax is used for all variables in Cheetah placeholders |
| 377 |
and directives. If desired, it can be turned off via the {Template} |
| 378 |
class' {'useNameMapper'} compiler setting. But it's doubtful you'd |
| 379 |
ever want to turn it off. |
| 380 |
|
| 381 |
Example |
| 382 |
~~~~~~~ |
| 383 |
|
| 384 |
|
| 385 |
Consider this scenario: |
| 386 |
|
| 387 |
You are building a customer information system. The designers with |
| 388 |
you want to use information from your system on the client's |
| 389 |
website -AND- they want to understand the display code and so they |
| 390 |
can maintian it themselves. |
| 391 |
|
| 392 |
You write a UI class with a 'customers' method that returns a |
| 393 |
dictionary of all the customer objects. Each customer object has an |
| 394 |
'address' method that returns the a dictionary with information |
| 395 |
about the customer's address. The designers want to be able to |
| 396 |
access that information. |
| 397 |
|
| 398 |
Using PSP, the display code for the website would look something |
| 399 |
like the following, assuming your servlet subclasses the class you |
| 400 |
created for managing customer information: |
| 401 |
|
| 402 |
:: |
| 403 |
|
| 404 |
<%= self.customer()[ID].address()['city'] %> (42 chars) |
| 405 |
|
| 406 |
With Cheetah's NameMapper syntax, you can use any of the |
| 407 |
following: |
| 408 |
|
| 409 |
:: |
| 410 |
|
| 411 |
$self.customers()[$ID].address()['city'] (39 chars) |
| 412 |
--OR-- |
| 413 |
$customers()[$ID].address()['city'] |
| 414 |
--OR-- |
| 415 |
$customers()[$ID].address().city |
| 416 |
--OR-- |
| 417 |
$customers()[$ID].address.city |
| 418 |
--OR-- |
| 419 |
$customers[$ID].address.city (27 chars) |
| 420 |
|
| 421 |
Which of these would you prefer to explain to the designers, who |
| 422 |
have no programming experience? The last form is 15 characters |
| 423 |
shorter than the PSP version and - conceptually - far more |
| 424 |
accessible. With PHP or ASP, the code would be even messier than |
| 425 |
with PSP. |
| 426 |
|
| 427 |
This is a rather extreme example and, of course, you could also |
| 428 |
just implement {$getCustomer($ID).city} and obey the Law of Demeter |
| 429 |
(search Google for more on that). But good object orientated design |
| 430 |
isn't the point of this example. |
| 431 |
|
| 432 |
Dictionary Access |
| 433 |
~~~~~~~~~~~~~~~~~ |
| 434 |
|
| 435 |
|
| 436 |
NameMapper syntax allows access to dictionary items with the same |
| 437 |
dotted notation used to access object attributes in Python. This |
| 438 |
aspect of NameMapper syntax is known as 'Unified Dotted Notation'. |
| 439 |
For example, with Cheetah it is possible to write: |
| 440 |
|
| 441 |
:: |
| 442 |
|
| 443 |
$customers()['kerr'].address() --OR-- $customers().kerr.address() |
| 444 |
|
| 445 |
where the second form is in NameMapper syntax. |
| 446 |
|
| 447 |
This works only with dictionary keys that also happen to be valid |
| 448 |
Python identifiers. |
| 449 |
|
| 450 |
Autocalling |
| 451 |
~~~~~~~~~~~ |
| 452 |
|
| 453 |
|
| 454 |
Cheetah automatically detects functions and methods in Cheetah |
| 455 |
$variables and calls them if the parentheses have been left off. |
| 456 |
Our previous example can be further simplified to: |
| 457 |
|
| 458 |
:: |
| 459 |
|
| 460 |
$customers.kerr.address |
| 461 |
|
| 462 |
As another example, if 'a' is an object, 'b' is a method |
| 463 |
|
| 464 |
:: |
| 465 |
|
| 466 |
$a.b |
| 467 |
|
| 468 |
is equivalent to |
| 469 |
|
| 470 |
:: |
| 471 |
|
| 472 |
$a.b() |
| 473 |
|
| 474 |
If b returns a dictionary, then following variations are possible |
| 475 |
|
| 476 |
:: |
| 477 |
|
| 478 |
$a.b.c --OR-- $a.b().c --OR-- $a.b()['c'] |
| 479 |
|
| 480 |
where 'c' is a key in the dictionary that a.b() returns. |
| 481 |
|
| 482 |
Further notes: |
| 483 |
|
| 484 |
|
| 485 |
- When Cheetah autocalls a function/method, it calls it without |
| 486 |
any arguments. Thus, the function/method must have been declared |
| 487 |
without arguments (except {self} for methods) or to provide default |
| 488 |
values for all arguments. If the function requires arguments, you |
| 489 |
must use the {()}. |
| 490 |
|
| 491 |
- Cheetah autocalls only functions and methods. Classes and other |
| 492 |
callable objects are not autocalled. The reason is that the primary |
| 493 |
purpose of a function/method is to call it, whereas the primary |
| 494 |
purpose of an instance is to look up its attributes or call its |
| 495 |
methods, not to call the instance itself. And calling a class may |
| 496 |
allocate large sums of memory uselessly or have other side effects, |
| 497 |
depending on the class. For instance, consider {$myInstance.fname}. |
| 498 |
Do we want to look up {fname} in the namespace of {myInstance} or |
| 499 |
in the namespace of whatever {myinstance} returns? It could go |
| 500 |
either way, so Cheetah follows the principle of least surprise. If |
| 501 |
you { do} want to call the instance, put the {()} on, or rename the |
| 502 |
{.\_\_call\_\_()} method to {.\_\_str\_\_}. |
| 503 |
|
| 504 |
- Autocalling can be disabled via Cheetah's 'useAutocalling' |
| 505 |
compiler setting. You can also disable it for one placeholder by |
| 506 |
using the syntax {$getVar('varName', 'default value', False)}. |
| 507 |
({.getVar()} works only with searchList values.) |
| 508 |
|
| 509 |
|
| 510 |
Namespace cascading and the searchList |
| 511 |
-------------------------------------- |
| 512 |
|
| 513 |
|
| 514 |
When Cheetah maps a variable name in a template to a Python value, |
| 515 |
it searches several namespaces in order: |
| 516 |
|
| 517 |
|
| 518 |
#. { Local variables:} created by {#set}, {#for}, or predefined by |
| 519 |
Cheetah. |
| 520 |
|
| 521 |
#. The { searchList}, consisting of: |
| 522 |
|
| 523 |
|
| 524 |
#. {#set global} variables. |
| 525 |
|
| 526 |
#. The { searchList} containers you passed to the {Template} |
| 527 |
constructor, if any. |
| 528 |
|
| 529 |
#. The { Template instance} ("self"). This contains any attributes |
| 530 |
you assigned, {#def} methods and {#block methods}, |
| 531 |
attributes/methods inherited via {#extends}, and other |
| 532 |
attributes/methods built into {Template} or inherited by it |
| 533 |
(there's a list of all these methods in section tips.allMethods). |
| 534 |
|
| 535 |
|
| 536 |
#. { Python globals:} created by {#import}, {#from ... import}, or |
| 537 |
otherwise predefined by Cheetah. |
| 538 |
|
| 539 |
#. { Python builtins:} {None}, {max}, etc. |
| 540 |
|
| 541 |
|
| 542 |
The first matching name found is used. |
| 543 |
|
| 544 |
Remember, these namespaces apply only to the { first} identifier |
| 545 |
after the {$}. In a placeholder like {$a.b}, only 'a' is looked up |
| 546 |
in the searchList and other namespaces. 'b' is looked up only |
| 547 |
inside 'a'. |
| 548 |
|
| 549 |
A searchList container can be any Python object with attributes or |
| 550 |
keys: dictionaries, instances, classes or modules. If an instance |
| 551 |
contains both attributes and keys, its attributes are searched |
| 552 |
first, then its keys. |
| 553 |
|
| 554 |
Because the {Template} instance is part of the searchList, you can |
| 555 |
access its attributes/methods without 'self': {$myAttr}. However, |
| 556 |
use the 'self' if you want to make sure you're getting the |
| 557 |
{Template} attribute and not a same-name variable defined in a |
| 558 |
higher namespace: {$self.myAttr}. This works because "self" itself |
| 559 |
is a local variable. |
| 560 |
|
| 561 |
The final resulting value, after all lookups and function calls |
| 562 |
(but before the filter is applied) is called the { placeholder |
| 563 |
value}, no matter which namespace it was found in. |
| 564 |
|
| 565 |
{ { Note carefully:}} if you put an object 'myObject' in the |
| 566 |
searchList, you { cannot} look up {$myObject}! You can look up only |
| 567 |
the attributes/keys { inside} 'myObject'. |
| 568 |
|
| 569 |
Earlier versions of Cheetah did not allow you to override Python |
| 570 |
builtin names, but this was fixed in Cheetah 0.9.15. |
| 571 |
|
| 572 |
If your template will be used as a Webware servlet, do not override |
| 573 |
methods 'name' and 'log' in the {Template} instance or it will |
| 574 |
interfere with Webware's logging. However, it { is} OK to use those |
| 575 |
variables in a higher namespace, since Webware doesn't know about |
| 576 |
Cheetah namespaces. |
| 577 |
|
| 578 |
Missing Values |
| 579 |
-------------- |
| 580 |
|
| 581 |
|
| 582 |
If NameMapper can not find a Python value for a Cheetah variable |
| 583 |
name, it will raise the NameMapper.NotFound exception. You can use |
| 584 |
the {#errorCatcher} directive (section errorHandling.errorCatcher) |
| 585 |
or { errorCatcher} Template constructor argument (section |
| 586 |
howWorks.constructing) to specify an alternate behaviour. BUT BE |
| 587 |
AWARE THAT errorCatcher IS ONLY INTENDED FOR DEBUGGING! |
| 588 |
|
| 589 |
To provide a default value for a placeholder, write it like this: |
| 590 |
{$getVar('varName', 'default value')}. If you don't specify a |
| 591 |
default and the variable is missing, {NameMapper.NotFound} will be |
| 592 |
raised. |
| 593 |
|
| 594 |
Directive Syntax Rules |
| 595 |
---------------------- |
| 596 |
|
| 597 |
|
| 598 |
Directive tags begin with a hash character (#) and are used for |
| 599 |
comments, loops, conditional blocks, includes, and all other |
| 600 |
advanced features. Cheetah uses a Python-like syntax inside |
| 601 |
directive tags and understands any valid Python expression. { |
| 602 |
However, unlike Python, Cheetah does not use colons (:) and |
| 603 |
indentation to mark off multi-line directives.} That doesn't work |
| 604 |
in an environment where whitespace is significant as part of the |
| 605 |
text. Instead, multi-line directives like {#for} have corresponding |
| 606 |
closing tags ({#end for}). Most directives are direct mirrors of |
| 607 |
Python statements. |
| 608 |
|
| 609 |
Many directives have arguments after the opening tag, which must be |
| 610 |
in the specified syntax for the tag. All end tags have the |
| 611 |
following syntax: |
| 612 |
|
| 613 |
:: |
| 614 |
|
| 615 |
#end TAG_NAME [EXPR] |
| 616 |
|
| 617 |
The expression is ignored, so it's essentially a comment. |
| 618 |
|
| 619 |
Directive closures and whitespace handling |
| 620 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 621 |
|
| 622 |
(language.directives.closures) Directive tags can be closed |
| 623 |
explicitly with {#}, or implicitly with the end of the line if |
| 624 |
you're feeling lazy. |
| 625 |
|
| 626 |
:: |
| 627 |
|
| 628 |
#block testBlock # |
| 629 |
Text in the body of the |
| 630 |
block directive |
| 631 |
#end block testBlock # |
| 632 |
|
| 633 |
is identical to: |
| 634 |
|
| 635 |
:: |
| 636 |
|
| 637 |
#block testBlock |
| 638 |
Text in the body of the |
| 639 |
block directive |
| 640 |
#end block testBlock |
| 641 |
|
| 642 |
When a directive tag is closed explicitly, it can be followed with |
| 643 |
other text on the same line: |
| 644 |
|
| 645 |
:: |
| 646 |
|
| 647 |
bah, bah, #if $sheep.color == 'black'# black#end if # sheep. |
| 648 |
|
| 649 |
When a directive tag is closed implicitly with the end of the line, |
| 650 |
all trailing whitespace is gobbled, including the newline |
| 651 |
character: |
| 652 |
|
| 653 |
:: |
| 654 |
|
| 655 |
""" |
| 656 |
foo #set $x = 2 |
| 657 |
bar |
| 658 |
""" |
| 659 |
outputs |
| 660 |
""" |
| 661 |
foo bar |
| 662 |
""" |
| 663 |
|
| 664 |
while |
| 665 |
""" |
| 666 |
foo #set $x = 2 # |
| 667 |
bar |
| 668 |
""" |
| 669 |
outputs |
| 670 |
""" |
| 671 |
foo |
| 672 |
bar |
| 673 |
""" |
| 674 |
|
| 675 |
When a directive tag is closed implicitly AND there is no other |
| 676 |
text on the line, the ENTIRE line is gobbled up including any |
| 677 |
preceeding whitespace: |
| 678 |
|
| 679 |
:: |
| 680 |
|
| 681 |
""" |
| 682 |
foo |
| 683 |
#set $x = 2 |
| 684 |
bar |
| 685 |
""" |
| 686 |
outputs |
| 687 |
""" |
| 688 |
foo |
| 689 |
bar |
| 690 |
""" |
| 691 |
|
| 692 |
while |
| 693 |
""" |
| 694 |
foo |
| 695 |
- #set $x = 2 |
| 696 |
bar |
| 697 |
""" |
| 698 |
outputs |
| 699 |
""" |
| 700 |
foo |
| 701 |
- bar |
| 702 |
""" |
| 703 |
|
| 704 |
The {#slurp} directive (section output.slurp) also gobbles up |
| 705 |
whitespace. |
| 706 |
|
| 707 |
Spaces outside directives are output { exactly} as written. In the |
| 708 |
black sheep example, there's a space before "black" and another |
| 709 |
before "sheep". So although it's legal to put multiple directives |
| 710 |
on one line, it can be hard to read. |
| 711 |
|
| 712 |
:: |
| 713 |
|
| 714 |
#if $a# #echo $a + 1# #end if |
| 715 |
- There's a space between each directive, |
| 716 |
or two extra spaces total. |
| 717 |
#if $a##echo $a + 1##end if |
| 718 |
- No spaces, but you have to look closely |
| 719 |
to verify none of the ``##'' are comment markers. |
| 720 |
#if $a##echo $a + 1##end if ### A comment. |
| 721 |
- In ``###'', the first ``#'' ends the directive, |
| 722 |
the other two begin the comment. (This also shows |
| 723 |
how you can add extra whitespace in the directive |
| 724 |
tag without affecting the output.) |
| 725 |
#if $a##echo $a + 1##end if # ## A comment. |
| 726 |
- More readable, but now there's a space before the |
| 727 |
comment. |
| 728 |
|
| 729 |
|