Debian Codegraph @

docs/users_guide/otherHtml.rst

raw
1 non-Webware HTML output
2 =======================
3
4
5 Cheetah can be used with all types of HTML output, not just with
6 Webware.
7
8 Static HTML Pages
9 -----------------
10
11
12 Some sites like Linux Gazette (http://www.linuxgazette.com/)
13 require completely static pages because they are mirrored on
14 servers running completely different software from the main site.
15 Even dynamic sites may have one or two pages that are static for
16 whatever reason, and the site administrator may wish to generate
17 those pages from Cheetah templates.
18
19 There's nothing special here. Just create your templates as usual.
20 Then compile and fill them whenever the template definition
21 changes, and fill them again whenever the placeholder values
22 change. You may need an extra step to copy the .html files to their
23 final location. A Makefile (chapter tips.Makefile) can help
24 encapsulate these steps.
25
26 CGI scripts
27 -----------
28
29
30 Unlike Webware servlets, which don't have to worry about the HTTP
31 headers, CGI scripts must emit their own headers. To make a
32 template CGI aware, add this at the top:
33
34 ::
35
36 #extends Cheetah.Tools.CGITemplate
37 #implements respond
38 $cgiHeaders#slurp
39
40 Or if your template is inheriting from a Python class:
41
42 ::
43
44 #extends MyPythonClass
45 #implements respond
46 $cgiHeaders#slurp
47
48 A sample Python class:
49
50 ::
51
52 from Cheetah.Tools import CGITemplate
53 class MyPythonClass(CGITemplate):
54 def cgiHeadersHook(self):
55 return "Content-Type: text/html; charset=koi8-r\n\n"
56
57 Compile the template as usual, put the .py template module in your
58 cgi-bin directory and give it execute permission. {.cgiHeaders()}
59 is a "smart" method that outputs the headers if the module is
60 called as a CGI script, or outputs nothing if not. Being
61 "called as a CGI script" means the environmental variable
62 {REQUEST\_METHOD} exists and {self.isControlledByWebKit} is false.
63 If you don't agree with that definition, override {.isCgi()} and
64 provide your own.
65
66 The default header is a simple ``Content-type: text/html\n\n``,
67 which works with all CGI scripts. If you want to customize the
68 headers (e.g., to specify the character set), override
69 {.cgiHeadersHook()} and return a string containing all the headers.
70 Don't forget to include the extra newline at the end of the string:
71 the HTTP protocol requires this empty line to mark the end of the
72 headers.
73
74 To read GET/POST variables from form input, use the {.webInput()}
75 method (section webware.webInput), or extract them yourself using
76 Python's {cgi} module or your own function. Although {.webInput()}
77 was originally written for Webware servlets, it now handles CGI
78 scripts too. There are a couple behavioral differences between CGI
79 scripts and Webware servlets regarding input variables:
80
81
82 #. CGI scripts, using Python's {cgi} module, believe
83 {REQUEST\_METHOD} and recognize { either} GET variables { or} POST
84 variables, not both. Webware servlets, doing additional processing,
85 ignore {REQUEST\_METHOD} and recognize both, like PHP does.
86
87 #. Webware servlets can ask for cookies or session variables
88 instead of GET/POST variables, by passing the argument {src='c'} or
89 {src='s'}. CGI scripts get a {RuntimeError} if they try to do
90 this.
91
92
93 If you keep your .tmpl files in the same directory as your CGI
94 scripts, make sure they don't have execute permission. Apache at
95 least refuses to serve files in a {ScriptAlias} directory that
96 don't have execute permission.
97
98