| 1 |
Getting Started |
| 2 |
=============== |
| 3 |
|
| 4 |
|
| 5 |
Requirements |
| 6 |
------------ |
| 7 |
|
| 8 |
|
| 9 |
Cheetah requires Python release 2.7 (there are plans to support 3.4+). |
| 10 |
It is known to run on Linux, Windows, FreeBSD and Solaris, |
| 11 |
and should run anywhere Python runs. |
| 12 |
|
| 13 |
99% of Cheetah is written in Python. There is one small C module |
| 14 |
({\_namemapper.so}) for speed, but Cheetah automatically falls back |
| 15 |
to a Python equivalent ({NameMapper.py}) if the C module is not |
| 16 |
available. |
| 17 |
|
| 18 |
Cheetah can use an additional module Markdown but it's not strictly required. |
| 19 |
|
| 20 |
Installation |
| 21 |
------------ |
| 22 |
|
| 23 |
|
| 24 |
To install Cheetah in your system-wide Python library: |
| 25 |
|
| 26 |
|
| 27 |
#. Login as a user with privileges to install system-wide Python |
| 28 |
packages. On POSIX systems (AIX, Solaris, Linux, IRIX, etc.), the |
| 29 |
command is normally 'su root'. On non-POSIX systems such as Windows |
| 30 |
NT, login as an administrator. |
| 31 |
|
| 32 |
#. Run {pip install CT3} at the command prompt. |
| 33 |
|
| 34 |
#. Or download source code and run {python setup.py install}. |
| 35 |
|
| 36 |
#. The setup program will install the wrapper script { cheetah} to |
| 37 |
wherever it usually puts Python binaries ("/usr/bin/", "bin/" in |
| 38 |
the Python install directory, etc.) |
| 39 |
|
| 40 |
#. If you cannot login as as an administrator install Cheetah as user to your |
| 41 |
own home directory: add option {--user} to commands: either |
| 42 |
{pip install --user CT3} or {python setup.py install --user}. |
| 43 |
|
| 44 |
Cheetah's installation is managed by Python's Distribution |
| 45 |
Utilities ('distutils'). There are many options for customization. |
| 46 |
Type {python setup.py help} for more information. |
| 47 |
|
| 48 |
To install Cheetah in an alternate location - someplace outside |
| 49 |
Python's {site-packages/} directory, use one of these options: |
| 50 |
|
| 51 |
:: |
| 52 |
|
| 53 |
python setup.py install --home /home/tavis |
| 54 |
python setup.py install --install-lib /home/tavis/lib/python |
| 55 |
|
| 56 |
Either way installs to /home/tavis/lib/python/Cheetah/ . Of course, |
| 57 |
/home/tavis/lib/python must be in your Python path in order for |
| 58 |
Python to find Cheetah. |
| 59 |
|
| 60 |
Files |
| 61 |
----- |
| 62 |
|
| 63 |
|
| 64 |
If you do the systemwide install, all Cheetah modules are installed |
| 65 |
in the { site-packages/Cheetah/} subdirectory of your standard |
| 66 |
library directory; e.g., |
| 67 |
/opt/Python2.2/lib/python2.2/site-packages/Cheetah. |
| 68 |
|
| 69 |
Two commands are installed in Python's {bin/} directory or a system |
| 70 |
bin directory: {cheetah} (section gettingStarted.cheetah) and |
| 71 |
{cheetah-compile} (section howWorks.cheetah-compile). |
| 72 |
|
| 73 |
Uninstalling |
| 74 |
------------ |
| 75 |
|
| 76 |
|
| 77 |
To uninstall Cheetah, merely delete the site-packages/Cheetah/ |
| 78 |
directory. Then delete the "cheetah" and "cheetah-compile" commands |
| 79 |
from whichever bin/ directory they were put in. |
| 80 |
|
| 81 |
The 'cheetah' command |
| 82 |
--------------------- |
| 83 |
|
| 84 |
|
| 85 |
Cheetah comes with a utility {cheetah} that provides a command-line |
| 86 |
interface to various housekeeping tasks. The command's first |
| 87 |
argument is the name of the task. The following commands are |
| 88 |
currently supported: |
| 89 |
|
| 90 |
:: |
| 91 |
|
| 92 |
cheetah compile [options] [FILES ...] : Compile template definitions |
| 93 |
cheetah fill [options] [FILES ...] : Fill template definitions |
| 94 |
cheetah help : Print this help message |
| 95 |
cheetah options : Print options help message |
| 96 |
cheetah test : Run Cheetah's regression tests |
| 97 |
cheetah version : Print Cheetah version number |
| 98 |
|
| 99 |
You only have to type the first letter of the command: {cheetah c} |
| 100 |
is the same as {cheetah compile}. |
| 101 |
|
| 102 |
The test suite is described in the next section. The {compile} |
| 103 |
command will be described in section howWorks.cheetah-compile, and |
| 104 |
the {fill} command in section howWorks.cheetah-fill. |
| 105 |
|
| 106 |
The depreciated {cheetah-compile} program does the same thing as |
| 107 |
{cheetah compile}. |
| 108 |
|
| 109 |
Testing your installation |
| 110 |
------------------------- |
| 111 |
|
| 112 |
|
| 113 |
After installing Cheetah, you can run its self-test routine to |
| 114 |
verify it's working properly on your system. Change directory to |
| 115 |
any directory you have write permission in (the tests write |
| 116 |
temporary files). Do not run the tests in the directory you |
| 117 |
installed Cheetah from, or you'll get unnecessary errors. Type the |
| 118 |
following at the command prompt: |
| 119 |
|
| 120 |
:: |
| 121 |
|
| 122 |
cheetah test |
| 123 |
|
| 124 |
The tests will run for about three minutes and print a |
| 125 |
success/failure message. If the tests pass, start Python in |
| 126 |
interactive mode and try the example in the next section. |
| 127 |
|
| 128 |
Certain test failures are insignificant: |
| 129 |
|
| 130 |
Certain tests run "cheetah" as a subcommand. The failure may mean |
| 131 |
the command wasn't found in your system path. (What happens if you |
| 132 |
run "cheetah" on the command line?) The failure also happens on |
| 133 |
some Windows systems for unknown reasons. This failure has never |
| 134 |
been observed outside the test suite. Long term, we plan to rewrite |
| 135 |
the tests to do a function call rather than a subcommand, which |
| 136 |
will also make the tests run significantly faster. |
| 137 |
|
| 138 |
The test tried to write a temporary module in the current directory |
| 139 |
and {import} it. Reread the first paragraph in this section about |
| 140 |
the current directory. |
| 141 |
|
| 142 |
May be the same problem as SampleBaseClass; let us know if changing |
| 143 |
the current directory doesn't work. |
| 144 |
|
| 145 |
|
| 146 |
If any other tests fail, please send a message to the e-mail list |
| 147 |
with a copy of the test output and the following details about your |
| 148 |
installation: |
| 149 |
|
| 150 |
|
| 151 |
#. your version of Cheetah |
| 152 |
|
| 153 |
#. your version of Python |
| 154 |
|
| 155 |
#. your operating system |
| 156 |
|
| 157 |
#. whether you have changed anything in the Cheetah installation |
| 158 |
|
| 159 |
|
| 160 |
Quickstart tutorial |
| 161 |
------------------- |
| 162 |
|
| 163 |
|
| 164 |
This tutorial briefly introduces how to use Cheetah from the Python |
| 165 |
prompt. The following chapters will discuss other ways to use |
| 166 |
templates and more of Cheetah's features. |
| 167 |
|
| 168 |
The core of Cheetah is the {Template} class in the |
| 169 |
{Cheetah.Template} module. The following example shows how to use |
| 170 |
the {Template} class in an interactive Python session. {t} is the |
| 171 |
Template instance. Lines prefixed with {>>>} and {...} are user |
| 172 |
input. The remaining lines are Python output. |
| 173 |
|
| 174 |
:: |
| 175 |
|
| 176 |
>>> from Cheetah.Template import Template |
| 177 |
>>> templateDef = """ |
| 178 |
... <HTML> |
| 179 |
... <HEAD><TITLE>$title</TITLE></HEAD> |
| 180 |
... <BODY> |
| 181 |
... $contents |
| 182 |
... ## this is a single-line Cheetah comment and won't appear in the output |
| 183 |
... #* This is a multi-line comment and won't appear in the output |
| 184 |
... blah, blah, blah |
| 185 |
... *# |
| 186 |
... </BODY> |
| 187 |
... </HTML>""" |
| 188 |
>>> nameSpace = {'title': 'Hello World Example', 'contents': 'Hello World!'} |
| 189 |
>>> t = Template(templateDef, searchList=[nameSpace]) |
| 190 |
>>> print t |
| 191 |
|
| 192 |
<HTML> |
| 193 |
<HEAD><TITLE>Hello World Example</TITLE></HEAD> |
| 194 |
<BODY> |
| 195 |
Hello World! |
| 196 |
</BODY> |
| 197 |
</HTML> |
| 198 |
>>> print t # print it as many times as you want |
| 199 |
[ ... same output as above ... ] |
| 200 |
>>> nameSpace['title'] = 'Example #2' |
| 201 |
>>> nameSpace['contents'] = 'Hiya Planet Earth!' |
| 202 |
>>> print t # Now with different plug-in values. |
| 203 |
<HTML> |
| 204 |
<HEAD><TITLE>Example #2</TITLE></HEAD> |
| 205 |
<BODY> |
| 206 |
Hiya Planet Earth! |
| 207 |
</BODY> |
| 208 |
</HTML> |
| 209 |
|
| 210 |
Since Cheetah is extremely flexible, you can achieve the same |
| 211 |
result this way: |
| 212 |
|
| 213 |
:: |
| 214 |
|
| 215 |
>>> t2 = Template(templateDef) |
| 216 |
>>> t2.title = 'Hello World Example!' |
| 217 |
>>> t2.contents = 'Hello World' |
| 218 |
>>> print t2 |
| 219 |
[ ... same output as the first example above ... ] |
| 220 |
>>> t2.title = 'Example #2' |
| 221 |
>>> t2.contents = 'Hello World!' |
| 222 |
>>> print t2 |
| 223 |
[ ... same as Example #2 above ... ] |
| 224 |
|
| 225 |
Or this way: |
| 226 |
|
| 227 |
:: |
| 228 |
|
| 229 |
>>> class Template3(Template): |
| 230 |
>>> title = 'Hello World Example!' |
| 231 |
>>> contents = 'Hello World!' |
| 232 |
>>> t3 = Template3(templateDef) |
| 233 |
>>> print t3 |
| 234 |
[ ... you get the picture ... ] |
| 235 |
|
| 236 |
The template definition can also come from a file instead of a |
| 237 |
string, as we will see in section howWorks.constructing. |
| 238 |
|
| 239 |
The above is all fine for short templates, but for long templates |
| 240 |
or for an application that depends on many templates in a |
| 241 |
hierarchy, it's easier to store the templates in separate \*.tmpl |
| 242 |
files and use the { cheetah compile} program to convert them into |
| 243 |
Python classes in their own modules. This will be covered in |
| 244 |
section howWorks.cheetah-compile. |
| 245 |
|
| 246 |
As an appetizer, we'll just briefly mention that you can store |
| 247 |
constant values { inside} the template definition, and they will be |
| 248 |
converted to attributes in the generated class. You can also create |
| 249 |
methods the same way. You can even use inheritance to arrange your |
| 250 |
templates in a hierarchy, with more specific templates overriding |
| 251 |
certain parts of more general templates (e.g., a "page" template |
| 252 |
overriding a sidebar in a "section" template). |
| 253 |
|
| 254 |
For the minimalists out there, here's a template definition, |
| 255 |
instantiation and filling all in one Python statement: |
| 256 |
|
| 257 |
:: |
| 258 |
|
| 259 |
>>> print Template("Templates are pretty useless without placeholders.") |
| 260 |
Templates are pretty useless without placeholders. |
| 261 |
|
| 262 |
You use a precompiled template the same way, except you don't |
| 263 |
provide a template definition since it was already established: |
| 264 |
|
| 265 |
:: |
| 266 |
|
| 267 |
from MyPrecompiledTemplate import MyPrecompiledTemplate |
| 268 |
t = MyPrecompiledTemplate() |
| 269 |
t.name = "Fred Flintstone" |
| 270 |
t.city = "Bedrock City" |
| 271 |
print t |
| 272 |
|
| 273 |
|