| 1 |
Batteries included: templates and other libraries |
| 2 |
================================================= |
| 3 |
|
| 4 |
|
| 5 |
Cheetah comes "batteries included" with libraries of templates, |
| 6 |
functions, classes and other objects you can use in your own |
| 7 |
programs. The different types are listed alphabetically below, |
| 8 |
followed by a longer description of the SkeletonPage framework. |
| 9 |
Some of the objects are classes for specific purposes (e.g., |
| 10 |
filters or error catchers), while others are standalone and can be |
| 11 |
used without Cheetah. |
| 12 |
|
| 13 |
If you develop any objects which are generally useful for Cheetah |
| 14 |
sites, please consider posting them on the wiki with an |
| 15 |
announcement on the mailing list so we can incorporate them into |
| 16 |
the standard library. That way, all Cheetah users will benefit, and |
| 17 |
it will encourage others to contribute their objects, which might |
| 18 |
include something you want. |
| 19 |
|
| 20 |
ErrorCatchers |
| 21 |
------------- |
| 22 |
|
| 23 |
|
| 24 |
Module {Cheetah.ErrorCatchers} contains error-handling classes |
| 25 |
suitable for the {#errorCatcher} directive. These are debugging |
| 26 |
tools that are not intended for use in production systems. See |
| 27 |
section errorHandling.errorCatcher for a description of the error |
| 28 |
catchers bundled with Cheetah. |
| 29 |
|
| 30 |
FileUtils |
| 31 |
--------- |
| 32 |
|
| 33 |
|
| 34 |
Module {Cheetah.FileUtils} contains generic functions and classes |
| 35 |
for doing bulk search-and-replace on several files, and for finding |
| 36 |
all the files in a directory hierarchy whose names match a glob |
| 37 |
pattern. |
| 38 |
|
| 39 |
Filters |
| 40 |
------- |
| 41 |
|
| 42 |
|
| 43 |
Module {Filters} contains filters suitable for the {#Filter} |
| 44 |
directive. See section output.filter for a description of the |
| 45 |
filters bundled with Cheetah. |
| 46 |
|
| 47 |
SettingsManager |
| 48 |
--------------- |
| 49 |
|
| 50 |
|
| 51 |
The {SettingsManager} class in the {Cheetah.SettingsManager} module |
| 52 |
is a baseclass that provides facilities for managing application |
| 53 |
settings. It facilitates the use of user-supplied configuration |
| 54 |
files to fine tune an application. A setting is a key/value pair |
| 55 |
that an application or component (e.g., a filter, or your own |
| 56 |
servlets) looks up and treats as a configuration value to modify |
| 57 |
its (the component's) behaviour. |
| 58 |
|
| 59 |
SettingsManager is designed to: |
| 60 |
|
| 61 |
|
| 62 |
- work well with nested settings dictionaries of any depth |
| 63 |
|
| 64 |
- read/write {.ini style config files} (or strings) |
| 65 |
|
| 66 |
- read settings from Python source files (or strings) so that |
| 67 |
complex Python objects can be stored in the application's settings |
| 68 |
dictionary. For example, you might want to store references to |
| 69 |
various classes that are used by the application, and plugins to |
| 70 |
the application might want to substitute one class for another. |
| 71 |
|
| 72 |
- allow sections in {.ini config files} to be extended by settings |
| 73 |
in Python source files. If a section contains a setting like |
| 74 |
"{importSettings=mySettings.py}", {SettingsManager} will merge all |
| 75 |
the settings defined in "{mySettings.py}" with the settings for |
| 76 |
that section that are defined in the {.ini config file}. |
| 77 |
|
| 78 |
- maintain the case of setting names, unlike the ConfigParser |
| 79 |
module |
| 80 |
|
| 81 |
|
| 82 |
Cheetah uses {SettingsManager} to manage its configuration |
| 83 |
settings. {SettingsManager} might also be useful in your own |
| 84 |
applications. See the source code and docstrings in the file |
| 85 |
{Cheetah/SettingsManager.py} for more information. |
| 86 |
|
| 87 |
Templates |
| 88 |
--------- |
| 89 |
|
| 90 |
|
| 91 |
Package {Cheetah.Templates} contains stock templates that you can |
| 92 |
either use as is, or extend by using the {#def} directive to |
| 93 |
redefine specific { blocks}. Currently, the only template in here |
| 94 |
is SkeletonPage, which is described in detail below in section |
| 95 |
libraries.templates.skeletonPage. (Contributed by Tavis Rudd.) |
| 96 |
|
| 97 |
Tools |
| 98 |
----- |
| 99 |
|
| 100 |
|
| 101 |
Package {Cheetah.Tools} contains functions and classes contributed |
| 102 |
by third parties. Some are Cheetah-specific but others are generic |
| 103 |
and can be used standalone. None of them are imported by any other |
| 104 |
Cheetah component; you can delete the Tools/ directory and Cheetah |
| 105 |
will function fine. |
| 106 |
|
| 107 |
Some of the items in Tools/ are experimental and have been placed |
| 108 |
there just to see how useful they will be, and whether they attract |
| 109 |
enough users to make refining them worthwhile (the tools, not the |
| 110 |
users :). |
| 111 |
|
| 112 |
Nothing in Tools/ is guaranteed to be: (A) tested, (B) reliable, |
| 113 |
(C) immune from being deleted in a future Cheetah version, or (D) |
| 114 |
immune from backwards-incompatable changes. If you depend on |
| 115 |
something in Tools/ on a production system, consider making a copy |
| 116 |
of it outside the Cheetah/ directory so that this version won't be |
| 117 |
lost when you upgrade Cheetah. Also, learn enough about Python and |
| 118 |
about the Tool so that you can maintain it and bugfix it if |
| 119 |
necessary. |
| 120 |
|
| 121 |
If anything in Tools/ is found to be necessary to Cheetah's |
| 122 |
operation (i.e., if another Cheetah component starts importing it), |
| 123 |
it will be moved to the {Cheetah.Utils} package. |
| 124 |
|
| 125 |
Current Tools include: |
| 126 |
|
| 127 |
an ambitious class useful when iterating over records of data |
| 128 |
({#for} loops), displaying one pageful of records at a time (with |
| 129 |
previous/next links), and printing summary statistics about the |
| 130 |
records or the current page. See {MondoReportDoc.txt} in the same |
| 131 |
directory as the module. Some features are not implemented yet. |
| 132 |
{MondoReportTest.py} is a test suite (and it shows there are |
| 133 |
currently some errors in MondoReport, hmm). Contributed by Mike |
| 134 |
Orr. |
| 135 |
|
| 136 |
Nothing, but in a friendly way. Good for filling in for objects you |
| 137 |
want to hide. If {$form.f1} is a RecursiveNull object, then |
| 138 |
{$form.f1.anything["you"].might("use")} will resolve to the empty |
| 139 |
string. You can also put a {RecursiveNull} instance at the end of |
| 140 |
the searchList to convert missing values to '' rather than raising |
| 141 |
a {NotFound} error or having a (less efficient) errorCatcher handle |
| 142 |
it. Of course, maybe you prefer to get a {NotFound} error... |
| 143 |
Contributed by Ian Bicking. |
| 144 |
|
| 145 |
Provides navigational links to this page's parents and children. |
| 146 |
The constructor takes a recursive list of (url,description) pairs |
| 147 |
representing a tree of hyperlinks to every page in the site (or |
| 148 |
section, or application...), and also a string containing the |
| 149 |
current URL. Two methods 'menuList' and 'crumbs' return |
| 150 |
output-ready HTML showing an indented menu (hierarchy tree) or |
| 151 |
crumbs list (Yahoo-style bar: home > grandparent > parent > |
| 152 |
currentURL). Contributed by Ian Bicking. |
| 153 |
|
| 154 |
|
| 155 |
Utils |
| 156 |
----- |
| 157 |
|
| 158 |
|
| 159 |
Package {Cheetah.Utils} contains non-Cheetah-specific functions and |
| 160 |
classes that are imported by other Cheetah components. Many of |
| 161 |
these utils can be used standalone in other applications too. |
| 162 |
|
| 163 |
Current Utils include: |
| 164 |
|
| 165 |
This is inherited by {Template} objects, and provides the method, |
| 166 |
{.cgiImport} method (section webware.cgiImport). |
| 167 |
|
| 168 |
A catch-all module for small functions. |
| 169 |
|
| 170 |
Raise 'thing' if it's a subclass of Exception, otherwise return it. |
| 171 |
Useful when one argument does double duty as a default value or an |
| 172 |
exception to throw. Contribyted by Mike Orr. |
| 173 |
|
| 174 |
Verifies the dictionary does not contain any keys not listed in |
| 175 |
'legalKeywords'. If it does, raise TypeError. Useful for checking |
| 176 |
the keyword arguments to a function. Contributed by Mike Orr. |
| 177 |
|
| 178 |
|
| 179 |
Not implemented yet, but will contain the {.uploadFile} method (or |
| 180 |
three methods) to "safely" copy a form-uploaded file to a local |
| 181 |
file, to a searchList variable, or return it. When finished, this |
| 182 |
will be inherited by {Template}, allowing all templates to do this. |
| 183 |
If you want this feature, read the docstring in the source and let |
| 184 |
us know on the mailing list what you'd like this method to do. |
| 185 |
Contributed by Mike Orr. |
| 186 |
|
| 187 |
Functions to verify the type of a user-supplied function argument. |
| 188 |
Contributed by Mike Orr. |
| 189 |
|
| 190 |
|
| 191 |
Cheetah.Templates.SkeletonPage |
| 192 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 193 |
|
| 194 |
|
| 195 |
A stock template class that may be useful for web developers is |
| 196 |
defined in the {Cheetah.Templates.SkeletonPage} module. The |
| 197 |
{SkeletonPage} template class is generated from the following |
| 198 |
Cheetah source code: |
| 199 |
|
| 200 |
:: |
| 201 |
|
| 202 |
##doc-module: A Skeleton HTML page template, that provides basic structure and utility methods. |
| 203 |
################################################################################ |
| 204 |
#extends Cheetah.Templates._SkeletonPage |
| 205 |
#implements respond |
| 206 |
################################################################################ |
| 207 |
#cache id='header' |
| 208 |
$docType |
| 209 |
$htmlTag |
| 210 |
<!-- This document was autogenerated by Cheetah (https://cheetahtemplate.org/). |
| 211 |
Do not edit it directly! |
| 212 |
|
| 213 |
Copyright $currentYr - $siteCopyrightName - All Rights Reserved. |
| 214 |
Feel free to copy any javascript or html you like on this site, |
| 215 |
provided you remove all links and/or references to $siteDomainName |
| 216 |
However, please do not copy any content or images without permission. |
| 217 |
|
| 218 |
$siteCredits |
| 219 |
|
| 220 |
--> |
| 221 |
|
| 222 |
|
| 223 |
#block writeHeadTag |
| 224 |
<head> |
| 225 |
<title>$title</title> |
| 226 |
$metaTags |
| 227 |
$stylesheetTags |
| 228 |
$javascriptTags |
| 229 |
</head> |
| 230 |
#end block writeHeadTag |
| 231 |
|
| 232 |
#end cache header |
| 233 |
################# |
| 234 |
|
| 235 |
$bodyTag |
| 236 |
|
| 237 |
#block writeBody |
| 238 |
This skeleton page has no flesh. Its body needs to be implemented. |
| 239 |
#end block writeBody |
| 240 |
|
| 241 |
</body> |
| 242 |
</html> |
| 243 |
|
| 244 |
You can redefine any of the blocks defined in this template by |
| 245 |
writing a new template that {#extends} SkeletonPage. (As you |
| 246 |
remember, using {#extends} makes your template implement the |
| 247 |
{.writeBody()} method instead of {.respond()} - which happens to be |
| 248 |
the same method SkeletonPage expects the page content to be (note |
| 249 |
the writeBody block in SkeletonPage).) |
| 250 |
|
| 251 |
:: |
| 252 |
|
| 253 |
#def bodyContents |
| 254 |
Here's my new body. I've got some flesh on my bones now. |
| 255 |
#end def bodyContents |
| 256 |
|
| 257 |
All of the $placeholders used in the {SkeletonPage} template |
| 258 |
definition are attributes or methods of the {SkeletonPage} class. |
| 259 |
You can reimplement them as you wish in your subclass. Please read |
| 260 |
the source code of the file {Cheetah/Templates/\_SkeletonPage.py} |
| 261 |
before doing so. |
| 262 |
|
| 263 |
You'll need to understand how to use the following methods of the |
| 264 |
{SkeletonPage} class: {$metaTags()}, {$stylesheetTags()}, |
| 265 |
{$javascriptTags()}, and {$bodyTag()}. They take the data you |
| 266 |
define in various attributes and renders them into HTML tags. |
| 267 |
|
| 268 |
|
| 269 |
- { metaTags()} - Returns a formatted vesion of the |
| 270 |
self.\_metaTags dictionary, using the formatMetaTags function from |
| 271 |
{\_SkeletonPage.py}. |
| 272 |
|
| 273 |
- { stylesheetTags()} - Returns a formatted version of the |
| 274 |
{self.\_stylesheetLibs} and {self.\_stylesheets} dictionaries. The |
| 275 |
keys in {self.\_stylesheets} must be listed in the order that they |
| 276 |
should appear in the list {self.\_stylesheetsOrder}, to ensure that |
| 277 |
the style rules are defined in the correct order. |
| 278 |
|
| 279 |
- { javascriptTags()} - Returns a formatted version of the |
| 280 |
{self.\_javascriptTags} and {self.\_javascriptLibs} dictionaries. |
| 281 |
Each value in {self.\_javascriptTags} should be a either a code |
| 282 |
string to include, or a list containing the JavaScript version |
| 283 |
number and the code string. The keys can be anything. The same |
| 284 |
applies for {self.\_javascriptLibs}, but the string should be the |
| 285 |
SRC filename rather than a code string. |
| 286 |
|
| 287 |
- { bodyTag()} - Returns an HTML body tag from the entries in the |
| 288 |
dict {self.\_bodyTagAttribs}. |
| 289 |
|
| 290 |
|
| 291 |
The class also provides some convenience methods that can be used |
| 292 |
as $placeholders in your template definitions: |
| 293 |
|
| 294 |
|
| 295 |
- { imgTag(self, src, alt='', width=None, height=None, border=0)} |
| 296 |
- Dynamically generate an image tag. Cheetah will try to convert |
| 297 |
the "{src}" argument to a WebKit serverSidePath relative to the |
| 298 |
servlet's location. If width and height aren't specified they are |
| 299 |
calculated using PIL or ImageMagick if either of these tools are |
| 300 |
available. If all your images are stored in a certain directory you |
| 301 |
can reimplement this method to append that directory's path to the |
| 302 |
"{src}" argument. Doing so would also insulate your template |
| 303 |
definitions from changes in your directory structure. |
| 304 |
|
| 305 |
|
| 306 |
|