1 | # $Id: CGITemplate.py,v 1.6 2006/01/29 02:09:59 tavis_rudd Exp $ |
---|
2 | """A subclass of Cheetah.Template for use in CGI scripts. |
---|
3 | |
---|
4 | Usage in a template: |
---|
5 | #extends Cheetah.Tools.CGITemplate |
---|
6 | #implements respond |
---|
7 | $cgiHeaders#slurp |
---|
8 | |
---|
9 | Usage in a template inheriting a Python class: |
---|
10 | 1. The template |
---|
11 | #extends MyPythonClass |
---|
12 | #implements respond |
---|
13 | $cgiHeaders#slurp |
---|
14 | |
---|
15 | 2. The Python class |
---|
16 | from Cheetah.Tools import CGITemplate |
---|
17 | class MyPythonClass(CGITemplate): |
---|
18 | def cgiHeadersHook(self): |
---|
19 | return "Content-Type: text/html; charset=koi8-r\n\n" |
---|
20 | |
---|
21 | To read GET/POST variables, use the .webInput method defined in |
---|
22 | Cheetah.Utils.WebInputMixin (available in all templates without importing |
---|
23 | anything), use Python's 'cgi' module, or make your own arrangements. |
---|
24 | |
---|
25 | This class inherits from Cheetah.Template to make it usable in Cheetah's |
---|
26 | single-inheritance model. |
---|
27 | |
---|
28 | |
---|
29 | Meta-Data |
---|
30 | ================================================================================ |
---|
31 | Author: Mike Orr <iron@mso.oz.net> |
---|
32 | License: This software is released for unlimited distribution under the |
---|
33 | terms of the MIT license. See the LICENSE file. |
---|
34 | Version: $Revision: 1.6 $ |
---|
35 | Start Date: 2001/10/03 |
---|
36 | Last Revision Date: $Date: 2006/01/29 02:09:59 $ |
---|
37 | """ |
---|
38 | __author__ = "Mike Orr <iron@mso.oz.net>" |
---|
39 | __revision__ = "$Revision: 1.6 $"[11:-2] |
---|
40 | |
---|
41 | import os |
---|
42 | from Cheetah.Template import Template |
---|
43 | |
---|
44 | class CGITemplate(Template): |
---|
45 | """Methods useful in CGI scripts. |
---|
46 | |
---|
47 | Any class that inherits this mixin must also inherit Cheetah.Servlet. |
---|
48 | """ |
---|
49 | |
---|
50 | |
---|
51 | def cgiHeaders(self): |
---|
52 | """Outputs the CGI headers if this is a CGI script. |
---|
53 | |
---|
54 | Usage: $cgiHeaders#slurp |
---|
55 | Override .cgiHeadersHook() if you want to customize the headers. |
---|
56 | """ |
---|
57 | if self.isCgi(): |
---|
58 | return self.cgiHeadersHook() |
---|
59 | |
---|
60 | |
---|
61 | |
---|
62 | def cgiHeadersHook(self): |
---|
63 | """Override if you want to customize the CGI headers. |
---|
64 | """ |
---|
65 | return "Content-type: text/html\n\n" |
---|
66 | |
---|
67 | |
---|
68 | def isCgi(self): |
---|
69 | """Is this a CGI script? |
---|
70 | """ |
---|
71 | env = os.environ.has_key('REQUEST_METHOD') |
---|
72 | wk = self._CHEETAH__isControlledByWebKit |
---|
73 | return env and not wk |
---|
74 | |
---|
75 | |
---|
76 | |
---|
77 | # vim: shiftwidth=4 tabstop=4 expandtab |
---|