| 1 |
Introduction |
| 2 |
=============== |
| 3 |
|
| 4 |
Who should read this Guide? |
| 5 |
--------------------------- |
| 6 |
|
| 7 |
This Users' Guide provides a technical overview and reference for |
| 8 |
the Cheetah template system. Knowledge of Python and |
| 9 |
object-oriented programming is assumed. The emphasis in this Guide |
| 10 |
is on features useful in a wide variety of situations. Information |
| 11 |
on less common situations and troubleshooting tips are gradually |
| 12 |
being moved to the Cheetah FAQ. There is also a Cheetah Developer's |
| 13 |
Guide for those who want to know what goes on under the hood. |
| 14 |
|
| 15 |
What is Cheetah? |
| 16 |
---------------- |
| 17 |
|
| 18 |
Cheetah is a Python-powered template engine and code generator. It |
| 19 |
may be used as a standalone utility or combined with other tools. |
| 20 |
Cheetah has many potential uses, but web developers looking for a |
| 21 |
viable alternative to ASP, JSP, PHP and PSP are expected to be its |
| 22 |
principle user group. |
| 23 |
|
| 24 |
Cheetah: |
| 25 |
|
| 26 |
|
| 27 |
- generates HTML, SGML, XML, SQL, Postscript, form email, LaTeX, |
| 28 |
or any other text-based format. It has also been used to produce |
| 29 |
Python, Java and PHP source code. |
| 30 |
|
| 31 |
- cleanly separates content, graphic design, and program code. |
| 32 |
This leads to highly modular, flexible, and reusable site |
| 33 |
architectures; faster development time; and HTML and program code |
| 34 |
that is easier to understand and maintain. It is particularly well |
| 35 |
suited for team efforts. |
| 36 |
|
| 37 |
- blends the power and flexibility of Python with a simple |
| 38 |
template language that non-programmers can understand. |
| 39 |
|
| 40 |
- gives template writers full access in their templates to any |
| 41 |
Python data structure, module, function, object, or method. |
| 42 |
|
| 43 |
- makes code reuse easy by providing an object-oriented interface |
| 44 |
to templates that is accessible from Python code or other Cheetah |
| 45 |
templates. One template can subclass another and selectively |
| 46 |
reimplement sections of it. A compiled template **is** a Python |
| 47 |
class, so it can subclass a pure Python class and vice-versa. |
| 48 |
|
| 49 |
- provides a simple yet powerful caching mechanism |
| 50 |
|
| 51 |
Like its namesake, Cheetah is fast, flexible and powerful. |
| 52 |
|
| 53 |
|
| 54 |
What is the philosophy behind Cheetah? |
| 55 |
-------------------------------------- |
| 56 |
Cheetah's design was guided by these principles: |
| 57 |
|
| 58 |
|
| 59 |
- Python for the back end, Cheetah for the front end. Cheetah was |
| 60 |
designed to complement Python, not replace it. |
| 61 |
|
| 62 |
- Cheetah's core syntax should be easy for non-programmers to |
| 63 |
learn. |
| 64 |
|
| 65 |
- Cheetah should make code reuse easy by providing an |
| 66 |
object-oriented interface to templates that is accessible from |
| 67 |
Python code or other Cheetah templates. |
| 68 |
|
| 69 |
- Python objects, functions, and other data structures should be |
| 70 |
fully accessible in Cheetah. |
| 71 |
|
| 72 |
- Cheetah should provide flow control and error handling. Logic |
| 73 |
that belongs in the front end shouldn't be relegated to the back |
| 74 |
end simply because it's complex. |
| 75 |
|
| 76 |
- It should be easy to **separate** content, graphic design, and |
| 77 |
program code, but also easy to **integrate** them. |
| 78 |
|
| 79 |
A clean separation makes it easier for a team of content writers, |
| 80 |
HTML/graphic designers, and programmers to work together without |
| 81 |
stepping on each other's toes and polluting each other's work. The |
| 82 |
HTML framework and the content it contains are two separate things, |
| 83 |
and analytical calculations (program code) is a third thing. Each |
| 84 |
team member should be able to concentrate on their specialty and to |
| 85 |
implement their changes without having to go through one of the |
| 86 |
others (i.e., the dreaded "webmaster bottleneck"). |
| 87 |
|
| 88 |
While it should be easy to develop content, graphics and program |
| 89 |
code separately, it should be easy to integrate them together into |
| 90 |
a website. In particular, it should be easy: |
| 91 |
|
| 92 |
|
| 93 |
- for **programmers** to create reusable components and functions |
| 94 |
that are accessible and understandable to designers. |
| 95 |
|
| 96 |
- for **designers** to mark out placeholders for content and |
| 97 |
dynamic components in their templates. |
| 98 |
|
| 99 |
- for **designers** to soft-code aspects of their design that are |
| 100 |
either repeated in several places or are subject to change. |
| 101 |
|
| 102 |
- for **designers** to reuse and extend existing templates and thus |
| 103 |
minimize duplication of effort and code. |
| 104 |
|
| 105 |
- and, of course, for **content writers** to use the templates that |
| 106 |
designers have created. |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
Why Cheetah doesn't use HTML-style tags |
| 111 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 112 |
|
| 113 |
Cheetah does not use HTML/XML-style tags like some other template |
| 114 |
languages for the following reasons: Cheetah is not limited to |
| 115 |
HTML, HTML-style tags are hard to distinguish from real HTML tags, |
| 116 |
HTML-style tags are not visible in rendered HTML when something |
| 117 |
goes wrong, HTML-style tags often lead to invalid HTML (e.g., ``<img |
| 118 |
src="<template-directive>">``), Cheetah tags are less verbose and |
| 119 |
easier to understand than HTML-style tags, and HTML-style tags |
| 120 |
aren't compatible with most WYSIWYG editors |
| 121 |
|
| 122 |
Besides being much more compact, Cheetah also has some advantages |
| 123 |
over languages that put information inside the HTML tags, such as |
| 124 |
Zope Page Templates or PHP: HTML or XML-bound languages do not work |
| 125 |
well with other languages, While ZPT-like syntaxes work well in |
| 126 |
many ways with WYSIWYG HTML editors, they also give up a |
| 127 |
significant advantage of those editors - concrete editing of the |
| 128 |
document. When logic is hidden away in (largely inaccessible) tags |
| 129 |
it is hard to understand a page simply by viewing it, and it is |
| 130 |
hard to confirm or modify that logic. |
| 131 |
|
| 132 |
Give me an example! |
| 133 |
------------------- |
| 134 |
|
| 135 |
Here's a very simple example that illustrates some of Cheetah's |
| 136 |
basic syntax: |
| 137 |
|
| 138 |
:: |
| 139 |
|
| 140 |
<HTML> |
| 141 |
<HEAD><TITLE>$title</TITLE></HEAD> |
| 142 |
<BODY> |
| 143 |
|
| 144 |
<TABLE> |
| 145 |
#for $client in $clients |
| 146 |
<TR> |
| 147 |
<TD>$client.surname, $client.firstname</TD> |
| 148 |
<TD><A HREF="mailto:$client.email">$client.email</A></TD> |
| 149 |
</TR> |
| 150 |
#end for |
| 151 |
</TABLE> |
| 152 |
|
| 153 |
</BODY> |
| 154 |
</HTML> |
| 155 |
|
| 156 |
Compare this with PSP: |
| 157 |
|
| 158 |
:: |
| 159 |
|
| 160 |
<HTML> |
| 161 |
<HEAD><TITLE><%=title%></TITLE></HEAD> |
| 162 |
<BODY> |
| 163 |
|
| 164 |
<TABLE> |
| 165 |
<% for client in clients: %> |
| 166 |
<TR> |
| 167 |
<TD><%=client['surname']%>, <%=client['firstname']%></TD> |
| 168 |
<TD><A HREF="mailto:<%=client['email']%>"><%=client['email']%></A></TD> |
| 169 |
</TR> |
| 170 |
<%end%> |
| 171 |
</TABLE> |
| 172 |
|
| 173 |
</BODY> |
| 174 |
</HTML> |
| 175 |
|
| 176 |
Section gettingStarted.tutorial has a more typical example that |
| 177 |
shows how to get the plug-in values **into** Cheetah, and section |
| 178 |
howWorks.cheetah-compile explains how to turn your template |
| 179 |
definition into an object-oriented Python module. |
| 180 |
|
| 181 |
Give me an example of a Webware servlet! |
| 182 |
---------------------------------------- |
| 183 |
|
| 184 |
This example uses an HTML form to ask the user's name, then invokes |
| 185 |
itself again to display a **personalized** friendly greeting. |
| 186 |
|
| 187 |
:: |
| 188 |
|
| 189 |
<HTML><HEAD><TITLE>My Template-Servlet</TITLE></HEAD><BODY> |
| 190 |
#set $name = $request.field('name', None) |
| 191 |
#if $name |
| 192 |
Hello $name |
| 193 |
#else |
| 194 |
<FORM ACTION="" METHOD="GET"> |
| 195 |
Name: <INPUT TYPE="text" NAME="name"><BR> |
| 196 |
<INPUT TYPE="submit"> |
| 197 |
</FORM> |
| 198 |
#end if |
| 199 |
</BODY></HTML> |
| 200 |
|
| 201 |
To try it out for yourself on a Webware system: |
| 202 |
|
| 203 |
|
| 204 |
#. copy the template definition to a file **test.tmpl** in your |
| 205 |
Webware servlet directory. |
| 206 |
|
| 207 |
#. Run ``cheetah compile test.tmpl``. This produces ``test.py`` (a |
| 208 |
.py template module) in the same directory. |
| 209 |
|
| 210 |
#. In your web browser, go to ``test.py``, using whatever site and |
| 211 |
directory is appropriate. |
| 212 |
|
| 213 |
At the first request, field 'name' will be blank (false) so the |
| 214 |
"#else" portion will execute and present a form. You type your name |
| 215 |
and press submit. The form invokes the same page. Now 'name' is |
| 216 |
true so the "#if" portion executes, which displays the greeting. |
| 217 |
The "#set" directive creates a local variable that lasts while the |
| 218 |
template is being filled. |
| 219 |
|
| 220 |
How mature is Cheetah? |
| 221 |
---------------------- |
| 222 |
|
| 223 |
Cheetah is stable, production quality, post-beta code. Cheetah's |
| 224 |
syntax, semantics and performance have been generally stable since |
| 225 |
a performance overhaul in mid 2001. Most of the changes since |
| 226 |
October 2001 have been in response to specific requests by |
| 227 |
production sites, things they need that we hadn't considered. |
| 228 |
|
| 229 |
As of summer 2003, we are putting in the final touches before the |
| 230 |
1.0 release. |
| 231 |
|
| 232 |
|
| 233 |
Where can I get news? |
| 234 |
--------------------- |
| 235 |
|
| 236 |
Cheetah releases can be obtained from the `Cheetah |
| 237 |
website <https://cheetahtemplate.org/>`_ |
| 238 |
|
| 239 |
If you encounter difficulties, or are unsure about how to do something, please |
| 240 |
post a detailed message to the `bug tracker |
| 241 |
<https://github.com/CheetahTemplate3/cheetah3/issues>`. |
| 242 |
|
| 243 |
How can I contribute? |
| 244 |
--------------------- |
| 245 |
|
| 246 |
Cheetah is the work of many volunteers. If you use Cheetah please |
| 247 |
share your experiences, tricks, customizations, and frustrations. |
| 248 |
|
| 249 |
Bug reports and patches |
| 250 |
~~~~~~~~~~~~~~~~~~~~~~~ |
| 251 |
|
| 252 |
If you think there is a bug in Cheetah, send a message to the |
| 253 |
e-mail list with the following information: |
| 254 |
|
| 255 |
|
| 256 |
#. a description of what you were trying to do and what happened |
| 257 |
|
| 258 |
#. all tracebacks and error output |
| 259 |
|
| 260 |
#. your version of Cheetah |
| 261 |
|
| 262 |
#. your version of Python |
| 263 |
|
| 264 |
#. your operating system |
| 265 |
|
| 266 |
#. whether you have changed anything in the Cheetah installation |
| 267 |
|
| 268 |
|
| 269 |
Template libraries and function libraries |
| 270 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 271 |
|
| 272 |
We hope to build up a framework of Template libraries (see section |
| 273 |
libraries.templates) to distribute with Cheetah and would |
| 274 |
appreciate any contributions. |
| 275 |
|
| 276 |
Test cases |
| 277 |
~~~~~~~~~~ |
| 278 |
|
| 279 |
Cheetah is packaged with a regression testing suite that is run |
| 280 |
with each new release to ensure that everything is working as |
| 281 |
expected and that recent changes haven't broken anything. The test |
| 282 |
cases are in the Cheetah.Tests module. If you find a reproduceable |
| 283 |
bug please consider writing a test case that will pass only when |
| 284 |
the bug is fixed. Send any new test cases to the email list with |
| 285 |
the subject-line "new test case for Cheetah." |
| 286 |
|
| 287 |
Publicity |
| 288 |
~~~~~~~~~ |
| 289 |
|
| 290 |
Help spread the word ... recommend it to others, write articles |
| 291 |
about it, etc. |
| 292 |
|
| 293 |
Acknowledgements |
| 294 |
---------------- |
| 295 |
|
| 296 |
Cheetah is one of several templating frameworks that grew out of a |
| 297 |
'templates' thread on the Webware For Python email list. Tavis |
| 298 |
Rudd, Mike Orr, Chuck Esterbrook and Ian Bicking are the core |
| 299 |
developers. |
| 300 |
|
| 301 |
We'd like to thank the following people for contributing valuable |
| 302 |
advice, code and encouragement: Geoff Talvola, Jeff Johnson, Graham |
| 303 |
Dumpleton, Clark C. Evans, Craig Kattner, Franz Geiger, Geir |
| 304 |
Magnusson, Tom Schwaller, Rober Kuzelj, Jay Love, Terrel Shumway, |
| 305 |
Sasa Zivkov, Arkaitz Bitorika, Jeremiah Bellomy, Baruch Even, Paul |
| 306 |
Boddie, Stephan Diehl, Chui Tey, Michael Halle, Edmund Lian and |
| 307 |
Aaron Held. |
| 308 |
|
| 309 |
The Velocity, WebMacro and Smarty projects provided inspiration and |
| 310 |
design ideas. Cheetah has benefitted from the creativity and energy |
| 311 |
of their developers. Thank you. |