| 1 |
.. role:: math(raw) |
| 2 |
:format: html latex |
| 3 |
|
| 4 |
Generating, Caching and Filtering Output |
| 5 |
======================================== |
| 6 |
|
| 7 |
|
| 8 |
Output from complex expressions: #echo |
| 9 |
-------------------------------------- |
| 10 |
|
| 11 |
|
| 12 |
Syntax: |
| 13 |
|
| 14 |
:: |
| 15 |
|
| 16 |
#echo EXPR |
| 17 |
|
| 18 |
The {#echo} directive is used to echo the output from expressions |
| 19 |
that can't be written as simple $placeholders. |
| 20 |
|
| 21 |
:: |
| 22 |
|
| 23 |
Here is my #echo ', '.join(['silly']*5) # example |
| 24 |
|
| 25 |
This produces: |
| 26 |
|
| 27 |
:: |
| 28 |
|
| 29 |
Here is my silly, silly, silly, silly, silly example. |
| 30 |
|
| 31 |
Executing expressions without output: #silent |
| 32 |
--------------------------------------------- |
| 33 |
|
| 34 |
|
| 35 |
Syntax: |
| 36 |
|
| 37 |
:: |
| 38 |
|
| 39 |
#silent EXPR |
| 40 |
|
| 41 |
{#silent} is the opposite of {#echo}. It executes an expression but |
| 42 |
discards the output. |
| 43 |
|
| 44 |
:: |
| 45 |
|
| 46 |
#silent $myList.reverse() |
| 47 |
#silent $myList.sort() |
| 48 |
Here is #silent $covertOperation() # nothing |
| 49 |
|
| 50 |
If your template requires some Python code to be executed at the |
| 51 |
beginning; (e.g., to calculate placeholder values, access a |
| 52 |
database, etc), you can put it in a "doEverything" method you |
| 53 |
inherit, and call this method using {#silent} at the top of the |
| 54 |
template. |
| 55 |
|
| 56 |
One-line #if |
| 57 |
------------ |
| 58 |
|
| 59 |
|
| 60 |
Syntax: |
| 61 |
|
| 62 |
:: |
| 63 |
|
| 64 |
#if EXPR1 then EXPR2 else EXPR3# |
| 65 |
|
| 66 |
The {#if} flow-control directive (section flowControl.if) has a |
| 67 |
one-line counterpart akin to Perl's and C's {?:} operator. If |
| 68 |
{EXPR1} is true, it evaluates {EXPR2} and outputs the result (just |
| 69 |
like {#echo EXPR2#}). Otherwise it evaluates {EXPR3} and outputs |
| 70 |
that result. This directive is short-circuiting, meaning the |
| 71 |
expression that isn't needed isn't evaluated. |
| 72 |
|
| 73 |
You MUST include both 'then' and 'else'. If this doesn't work for |
| 74 |
you or you don't like the style use multi-line {#if} directives |
| 75 |
(section flowControl.if). |
| 76 |
|
| 77 |
The trailing {#} is the normal end-of-directive character. As usual |
| 78 |
it may be omitted if there's nothing after the directive on the |
| 79 |
same line. |
| 80 |
|
| 81 |
Caching Output |
| 82 |
-------------- |
| 83 |
|
| 84 |
|
| 85 |
Caching individual placeholders |
| 86 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 87 |
|
| 88 |
|
| 89 |
By default, the values of each $placeholder is retrieved and |
| 90 |
interpolated for every request. However, it's possible to cache the |
| 91 |
values of individual placeholders if they don't change very often, |
| 92 |
in order to speed up the template filling. |
| 93 |
|
| 94 |
To cache the value of a single {$placeholder}, add an asterisk |
| 95 |
after the $; e.g., {$\*var}. The first time the template is filled, |
| 96 |
{$var} is looked up. Then whenever the template is filled again, |
| 97 |
the cached value is used instead of doing another lookup. |
| 98 |
|
| 99 |
The {$\*} format caches "forever"; that is, as long as the template |
| 100 |
instance remains in memory. It's also possible to cache for a |
| 101 |
certain time period using the form {$\*<interval>\*variable}, where |
| 102 |
{<interval>} is the interval. The time interval can be specified in |
| 103 |
seconds (5s), minutes (15m), hours (3h), days (2d) or weeks (1.5w). |
| 104 |
The default is minutes. |
| 105 |
|
| 106 |
:: |
| 107 |
|
| 108 |
<HTML> |
| 109 |
<HEAD><TITLE>$title</TITLE></HEAD> |
| 110 |
<BODY> |
| 111 |
|
| 112 |
$var ${var} ## dynamic - will be reinterpolated for each request |
| 113 |
$*var2 $*{var2} ## static - will be interpolated only once at start-up |
| 114 |
$*5*var3 $*5*{var3} ## timed refresh - will be updated every five minutes. |
| 115 |
|
| 116 |
</BODY> |
| 117 |
</HTML> |
| 118 |
|
| 119 |
Note that "every five minutes" in the example really means every |
| 120 |
five minutes: the variable is looked up again when the time limit |
| 121 |
is reached, whether the template is being filled that frequently or |
| 122 |
not. Keep this in mind when setting refresh times for CPU-intensive |
| 123 |
or I/O intensive operations. |
| 124 |
|
| 125 |
If you're using the long placeholder syntax, ``${}``, the braces go |
| 126 |
only around the placeholder name: ``$*.5h*{var.func('arg')}``. |
| 127 |
|
| 128 |
Sometimes it's preferable to explicitly invalidate a cached item |
| 129 |
whenever you say so rather than at certain time intervals. You |
| 130 |
can't do this with individual placeholders, but you can do it with |
| 131 |
cached regions, which will be described next. |
| 132 |
|
| 133 |
Caching entire regions |
| 134 |
~~~~~~~~~~~~~~~~~~~~~~ |
| 135 |
|
| 136 |
|
| 137 |
Syntax: |
| 138 |
|
| 139 |
:: |
| 140 |
|
| 141 |
#cache [id=EXPR] [timer=EXPR] [test=EXPR] |
| 142 |
#end cache |
| 143 |
|
| 144 |
The {#cache} directive is used to cache a region of content in a |
| 145 |
template. The region is cached as a single unit, after placeholders |
| 146 |
and directives inside the region have been evaluated. If there are |
| 147 |
any {$\*<interval>\*var} placholders inside the cache region, they |
| 148 |
are refreshed only when { both} the cache region { and} the |
| 149 |
placeholder are simultaneously due for a refresh. |
| 150 |
|
| 151 |
Caching regions offers more flexibility than caching individual |
| 152 |
placeholders. You can specify the refresh interval using a |
| 153 |
placeholder or expression, or refresh according to other criteria |
| 154 |
rather than a certain time interval. |
| 155 |
|
| 156 |
{#cache} without arguments caches the region statically, the same |
| 157 |
way as {$\*var}. The region will not be automatically refreshed. |
| 158 |
|
| 159 |
To refresh the region at an interval, use the {timer=EXPRESSION} |
| 160 |
argument, equivalent to {$\*<interval>\*}. The expression should |
| 161 |
evaluate to a number or string that is a valid interval (e.g., 0.5, |
| 162 |
'3m', etc). |
| 163 |
|
| 164 |
To refresh whenever an expression is true, use {test=EXPRESSION}. |
| 165 |
The expression can be a method/function returning true or false, a |
| 166 |
boolean placeholder, several of these joined by {and} and/or {or}, |
| 167 |
or any other expression. If the expression contains spaces, it's |
| 168 |
easier to read if you enclose it in {()}, but this is not |
| 169 |
required. |
| 170 |
|
| 171 |
To refresh whenever you say so, use {id=EXPRESSION}. Your program |
| 172 |
can then call {.refreshCache(ID)} whenever it wishes. This is |
| 173 |
useful if the cache depends on some external condition that changes |
| 174 |
infrequently but has just changed now. |
| 175 |
|
| 176 |
You can combine arguments by separating them with commas. For |
| 177 |
instance, you can specify both {id=} and {interval=}, or {id=} and |
| 178 |
{test=}. (You can also combine interval and test although it's not |
| 179 |
very useful.) However, repeating an argument is undefined. |
| 180 |
|
| 181 |
:: |
| 182 |
|
| 183 |
#cache |
| 184 |
This is a static cache. It will not be refreshed. |
| 185 |
$a $b $c |
| 186 |
#end cache |
| 187 |
|
| 188 |
#cache timer='30m', id='cache1' |
| 189 |
#for $cust in $customers |
| 190 |
$cust.name: |
| 191 |
$cust.street - $cust.city |
| 192 |
#end for |
| 193 |
#end cache |
| 194 |
|
| 195 |
#cache id='sidebar', test=$isDBUpdated |
| 196 |
... left sidebar HTML ... |
| 197 |
#end cache |
| 198 |
|
| 199 |
#cache id='sidebar2', test=($isDBUpdated or $someOtherCondition) |
| 200 |
... right sidebar HTML ... |
| 201 |
#end cache |
| 202 |
|
| 203 |
The {#cache} directive cannot be nested. |
| 204 |
|
| 205 |
We are planning to add a {'varyBy'} keyword argument in the future |
| 206 |
that will allow a separate cache instances to be created for a |
| 207 |
variety of conditions, such as different query string parameters or |
| 208 |
browser types. This is inspired by ASP.net's varyByParam and |
| 209 |
varyByBrowser output caching keywords. |
| 210 |
|
| 211 |
#raw |
| 212 |
---- |
| 213 |
|
| 214 |
|
| 215 |
Syntax: |
| 216 |
|
| 217 |
:: |
| 218 |
|
| 219 |
#raw |
| 220 |
#end raw |
| 221 |
|
| 222 |
Any section of a template definition that is inside a {#raw ... |
| 223 |
#end raw} tag pair will be printed verbatim without any parsing of |
| 224 |
$placeholders or other directives. This can be very useful for |
| 225 |
debugging, or for Cheetah examples and tutorials. |
| 226 |
|
| 227 |
{#raw} is conceptually similar to HTML's {<PRE>} tag and LaTeX's { |
| 228 |
verbatim{}} tag, but unlike those tags, {#raw} does not cause the |
| 229 |
body to appear in a special font or typeface. It can't, because |
| 230 |
Cheetah doesn't know what a font is. |
| 231 |
|
| 232 |
#include |
| 233 |
-------- |
| 234 |
|
| 235 |
|
| 236 |
Syntax: |
| 237 |
|
| 238 |
:: |
| 239 |
|
| 240 |
#include [raw] FILENAME_EXPR |
| 241 |
#include [raw] source=STRING_EXPR |
| 242 |
|
| 243 |
The {#include} directive is used to include text from outside the |
| 244 |
template definition. The text can come from an external file or |
| 245 |
from a {$placeholder} variable. When working with external files, |
| 246 |
Cheetah will monitor for changes to the included file and update as |
| 247 |
necessary. |
| 248 |
|
| 249 |
This example demonstrates its use with external files: |
| 250 |
|
| 251 |
:: |
| 252 |
|
| 253 |
#include "includeFileName.txt" |
| 254 |
|
| 255 |
The content of "includeFileName.txt" will be parsed for Cheetah |
| 256 |
syntax. |
| 257 |
|
| 258 |
And this example demonstrates use with {$placeholder} variables: |
| 259 |
|
| 260 |
:: |
| 261 |
|
| 262 |
#include source=$myParseText |
| 263 |
|
| 264 |
The value of {$myParseText} will be parsed for Cheetah syntax. This |
| 265 |
is not the same as simply placing the $placeholder tag |
| 266 |
"{$myParseText}" in the template definition. In the latter case, |
| 267 |
the value of $myParseText would not be parsed. |
| 268 |
|
| 269 |
By default, included text will be parsed for Cheetah tags. The |
| 270 |
argument "{raw}" can be used to suppress the parsing. |
| 271 |
|
| 272 |
:: |
| 273 |
|
| 274 |
#include raw "includeFileName.txt" |
| 275 |
#include raw source=$myParseText |
| 276 |
|
| 277 |
Cheetah wraps each chunk of {#include} text inside a nested |
| 278 |
{Template} object. Each nested template has a copy of the main |
| 279 |
template's searchList. However, {#set} variables are visible across |
| 280 |
includes only if the defined using the {#set global} keyword. |
| 281 |
|
| 282 |
All directives must be balanced in the include file. That is, if |
| 283 |
you start a {#for} or {#if} block inside the include, you must end |
| 284 |
it in the same include. (This is unlike PHP, which allows |
| 285 |
unbalanced constructs in include files.) |
| 286 |
|
| 287 |
#slurp |
| 288 |
------ |
| 289 |
|
| 290 |
|
| 291 |
Syntax: |
| 292 |
|
| 293 |
:: |
| 294 |
|
| 295 |
#slurp |
| 296 |
|
| 297 |
The {#slurp} directive eats up the trailing newline on the line it |
| 298 |
appears in, joining the following line onto the current line. |
| 299 |
|
| 300 |
It is particularly useful in {#for} loops: |
| 301 |
|
| 302 |
:: |
| 303 |
|
| 304 |
#for $i in range(5) |
| 305 |
$i #slurp |
| 306 |
#end for |
| 307 |
|
| 308 |
outputs: |
| 309 |
|
| 310 |
:: |
| 311 |
|
| 312 |
0 1 2 3 4 |
| 313 |
|
| 314 |
#indent |
| 315 |
------- |
| 316 |
|
| 317 |
|
| 318 |
This directive is not implemented yet. When/if it's completed, it |
| 319 |
will allow you to |
| 320 |
|
| 321 |
|
| 322 |
#. indent your template definition in a natural way (e.g., the |
| 323 |
bodies of {#if} blocks) without affecting the output |
| 324 |
|
| 325 |
#. add indentation to output lines without encoding it literally in |
| 326 |
the template definition. This will make it easier to use Cheetah to |
| 327 |
produce indented source code programmatically (e.g., Java or Python |
| 328 |
source code). |
| 329 |
|
| 330 |
|
| 331 |
There is some experimental code that recognizes the {#indent} |
| 332 |
directive with options, but the options are purposely undocumented |
| 333 |
at this time. So pretend it doesn't exist. If you have a use for |
| 334 |
this feature and would like to see it implemented sooner rather |
| 335 |
than later, let us know on the mailing list. |
| 336 |
|
| 337 |
The latest specification for the future {#indent} directive is in |
| 338 |
the TODO file in the Cheetah source distribution. |
| 339 |
|
| 340 |
Ouput Filtering and #filter |
| 341 |
--------------------------- |
| 342 |
|
| 343 |
|
| 344 |
Syntax: |
| 345 |
|
| 346 |
:: |
| 347 |
|
| 348 |
#filter FILTER_CLASS_NAME |
| 349 |
#filter $PLACEHOLDER_TO_A_FILTER_INSTANCE |
| 350 |
#filter None |
| 351 |
|
| 352 |
Output from $placeholders is passed through an ouput filter. The |
| 353 |
default filter merely returns a string representation of the |
| 354 |
placeholder value, unless the value is {None}, in which case the |
| 355 |
filter returns an empty string. Only top-level placeholders invoke |
| 356 |
the filter; placeholders inside expressions do not. |
| 357 |
|
| 358 |
Certain filters take optional arguments to modify their behaviour. |
| 359 |
To pass arguments, use the long placeholder syntax and precede each |
| 360 |
filter argument by a comma. By convention, filter arguments don't |
| 361 |
take a {$} prefix, to avoid clutter in the placeholder tag which |
| 362 |
already has plenty of dollar signs. For instance, the MaxLen filter |
| 363 |
takes an argument 'maxlen': |
| 364 |
|
| 365 |
:: |
| 366 |
|
| 367 |
${placeholderName, maxlen=20} |
| 368 |
${functionCall($functionArg), maxlen=$myMaxLen} |
| 369 |
|
| 370 |
To change the output filter, use the {'filter'} keyword to the |
| 371 |
{Template} class constructor, or the {#filter} directive at runtime |
| 372 |
(details below). You may use {#filter} as often as you wish to |
| 373 |
switch between several filters, if certain {$placeholders} need one |
| 374 |
filter and other {$placeholders} need another. |
| 375 |
|
| 376 |
The standard filters are in the module {Cheetah.Filters}. Cheetah |
| 377 |
currently provides: |
| 378 |
|
| 379 |
The default filter, which converts None to '' and everything else |
| 380 |
to {str(whateverItIs)}. This is the base class for all other |
| 381 |
filters, and the minimum behaviour for all filters distributed with |
| 382 |
Cheetah. |
| 383 |
|
| 384 |
Same. |
| 385 |
|
| 386 |
Same, but truncate the value if it's longer than a certain length. |
| 387 |
Use the 'maxlen' filter argument to specify the length, as in the |
| 388 |
examples above. If you don't specify 'maxlen', the value will not |
| 389 |
be truncated. |
| 390 |
|
| 391 |
Output a "pageful" of a long string. After the page, output HTML |
| 392 |
hyperlinks to the previous and next pages. This filter uses several |
| 393 |
filter arguments and environmental variables, which have not been |
| 394 |
documented yet. |
| 395 |
|
| 396 |
Same as default, but convert HTML-sensitive characters |
| 397 |
(':math:`$<$`', '&', ':math:`$>$`') to HTML entities so that the |
| 398 |
browser will display them literally rather than interpreting them |
| 399 |
as HTML tags. This is useful with database values or user input |
| 400 |
that may contain sensitive characters. But if your values contain |
| 401 |
embedded HTML tags you want to preserve, you do not want this |
| 402 |
filter. |
| 403 |
|
| 404 |
The filter argument 'also' may be used to specify additional |
| 405 |
characters to escape. For instance, say you want to ensure a value |
| 406 |
displays all on one line. Escape all spaces in the value with |
| 407 |
' ', the non-breaking space: |
| 408 |
|
| 409 |
:: |
| 410 |
|
| 411 |
${$country, also=' '}} |
| 412 |
|
| 413 |
|
| 414 |
To switch filters using a class object, pass the class using the { |
| 415 |
filter} argument to the Template constructor, or via a placeholder |
| 416 |
to the {#filter} directive: {#filter $myFilterClass}. The class |
| 417 |
must be a subclass of {Cheetah.Filters.Filter}. When passing a |
| 418 |
class object, the value of { filtersLib} does not matter, and it |
| 419 |
does not matter where the class was defined. |
| 420 |
|
| 421 |
To switch filters by name, pass the name of the class as a string |
| 422 |
using the { filter} argument to the Template constructor, or as a |
| 423 |
bare word (without quotes) to the {#filter} directive: {#filter |
| 424 |
TheFilter}. The class will be looked up in the { filtersLib}. |
| 425 |
|
| 426 |
The filtersLib is a module containing filter classes, by default |
| 427 |
{Cheetah.Filters}. All classes in the module that are subclasses of |
| 428 |
{Cheetah.Filters.Filter} are considered filters. If your filters |
| 429 |
are in another module, pass the module object as the { filtersLib} |
| 430 |
argument to the Template constructor. |
| 431 |
|
| 432 |
Writing a custom filter is easy: just override the {.filter} |
| 433 |
method. |
| 434 |
|
| 435 |
:: |
| 436 |
|
| 437 |
def filter(self, val, **kw): # Returns a string. |
| 438 |
|
| 439 |
Return the { string} that should be output for 'val'. 'val' may be |
| 440 |
any type. Most filters return \`' for {None}. Cheetah passes one |
| 441 |
keyword argument: ``kw['rawExpr']`` is the placeholder name as it |
| 442 |
appears in the template definition, including all subscripts and |
| 443 |
arguments. If you use the long placeholder syntax, any options you |
| 444 |
pass appear as keyword arguments. Again, the return value must be a |
| 445 |
string. |
| 446 |
|
| 447 |
You can always switch back to the default filter this way: {#filter |
| 448 |
None}. This is easy to remember because "no filter" means the |
| 449 |
default filter, and because None happens to be the only object the |
| 450 |
default filter treats specially. |
| 451 |
|
| 452 |
We are considering additional filters; see |
| 453 |
http://webware.colorstudy.net/twiki/bin/view/Cheetah/MoreFilters |
| 454 |
for the latest ideas. |
| 455 |
|
| 456 |
|