| 1 |
"""A subclass of Cheetah.Template for use in CGI scripts. |
| 2 |
|
| 3 |
Usage in a template: |
| 4 |
#extends Cheetah.Tools.CGITemplate |
| 5 |
#implements respond |
| 6 |
$cgiHeaders#slurp |
| 7 |
|
| 8 |
Usage in a template inheriting a Python class: |
| 9 |
1. The template |
| 10 |
#extends MyPythonClass |
| 11 |
#implements respond |
| 12 |
$cgiHeaders#slurp |
| 13 |
|
| 14 |
2. The Python class |
| 15 |
from Cheetah.Tools import CGITemplate |
| 16 |
class MyPythonClass(CGITemplate): |
| 17 |
def cgiHeadersHook(self): |
| 18 |
return "Content-Type: text/html; charset=koi8-r\n\n" |
| 19 |
|
| 20 |
To read GET/POST variables, use the .webInput method defined in |
| 21 |
Cheetah.Utils.WebInputMixin (available in all templates without importing |
| 22 |
anything), use Python's 'cgi' module, or make your own arrangements. |
| 23 |
|
| 24 |
This class inherits from Cheetah.Template to make it usable in Cheetah's |
| 25 |
single-inheritance model. |
| 26 |
""" |
| 27 |
|
| 28 |
import os |
| 29 |
from Cheetah.Template import Template |
| 30 |
|
| 31 |
|
| 32 |
class CGITemplate(Template): |
| 33 |
"""Methods useful in CGI scripts. |
| 34 |
|
| 35 |
Any class that inherits this mixin must also inherit Cheetah.Servlet. |
| 36 |
""" |
| 37 |
|
| 38 |
def cgiHeaders(self): |
| 39 |
"""Outputs the CGI headers if this is a CGI script. |
| 40 |
|
| 41 |
Usage: $cgiHeaders#slurp |
| 42 |
Override .cgiHeadersHook() if you want to customize the headers. |
| 43 |
""" |
| 44 |
if self.isCgi(): |
| 45 |
return self.cgiHeadersHook() |
| 46 |
|
| 47 |
def cgiHeadersHook(self): |
| 48 |
"""Override if you want to customize the CGI headers. |
| 49 |
""" |
| 50 |
return "Content-type: text/html\n\n" |
| 51 |
|
| 52 |
def isCgi(self): |
| 53 |
"""Is this a CGI script? |
| 54 |
""" |
| 55 |
env = 'REQUEST_METHOD' in os.environ |
| 56 |
wk = self._CHEETAH__isControlledByWebKit |
| 57 |
return env and not wk |
| 58 |
|
| 59 |
|
| 60 |
|