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