CherryPy Project Download

root/kidrender/__init__.py

Revision 3 (checked in by jtate, 2 years ago)

Revise kidrender from feedback given in #cherrypy

Line 
1 # vim: set fileencoding=utf-8 :
2 # Copyright (c) 2007 CherryPy Team
3 # Licensed under the same terms as CherryPy.
4
5 # To use this tool
6 # cherrypy.tools.kidrender = KidRender()
7 # class foo(object):
8 #     @cherrypy.expose
9 #     @cherrypy.tools.kidrender(template = 'kidservtemplate',
10 #                         output='xhtml-strict', encoding='utf-8',
11 #                         assume_encoding='utf-8')
12 #     def index(self):
13 #         return dict(title=u'Tést yøur brôüuser', text=u'¡Qué tal mundo, Dígamelo!')
14
15 # Sample template
16 #  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
17 #  <html xmlns="http://www.w3.org/1999/xhtml" xmlns:py="http://purl.org/kid/ns#">
18
19 #  <head>
20 #      <title>${title}</title>
21 #  </head>
22
23 #  <body>
24 #      ${text}
25 #  </body>
26
27 #  </html>
28
29
30 import cherrypy
31
32 import kid
33
34 class KidRender(cherrypy.Tool):
35     def __init__(self, name=None, priority = 0):
36         self._name = name
37         self._point = 'before_handler'
38         self._priority = priority
39         self._templatecache = {}
40
41     def callable(self, template, output=None, encoding='utf-8',
42                  assume_encoding='utf-8'):
43         '''
44             A simple template rendering mechanism using a trivial cache.  By
45             default generates xhtml output using utf-8 encoding.  The
46             dictionary returned by your exposed method is transformed by kid
47             into the proper output.
48
49             Most of the time the defaults are good enough
50
51             @param template: The name of the template to be imported
52             @param output: The type transform to perform.  See
53                 http://kid-templating.org/guide.html#common-output-methods for
54                 possible values.
55             @param encoding: Character encoding to use in the output
56             @param assume_encoding: Format to use when parsing the kid template
57         '''
58         cherrypy.request._handler = cherrypy.request.handler
59         def wrap(*args, **kwargs):
60             ret = cherrypy.request._handler(*args, **kwargs)
61             templ = self._templatecache.get(template, None)
62             if not templ:
63                 templ = kid.import_template(name=template)
64                 self._templatecache[template] = templ
65             t = templ.Template(**ret)
66             t.assume_encoding=assume_encoding
67             out = t.serialize(output=output, encoding=encoding)
68             if output is None or 'xhtml' in output:
69                 if 'application/xhtml+xml' in cherrypy.request.headers['Accept']:
70                     cherrypy.response.headers['Content-Type'] = 'application/xhtml+xml'
71                 else:
72                     #This is nasty, but such is the state of XHTML today
73                     cherrypy.response.headers['Content-Type'] = 'text/html; charset=%s' % encoding
74             return out
75
76         cherrypy.request.handler = wrap
77
Note: See TracBrowser for help on using the browser.

Hosted by WebFaction

Log in as guest/cherrypy to create/edit wiki pages