Genshi server pages

15 July 2008
21:37

By cmlenz as Python

Serving “static” Genshi templates from mod_wsgi

1 from genshi.template import TemplateLoader, TemplateNotFound
2
3 loader = TemplateLoader('/path/to/templates')
4
5 data = {
6 'site_name': 'Foo bar'
7 }
8
9 def application(environ, start_response):
10 try:
11 path = environ['PATH_INFO'].lstrip('/')
12 try:
13 tmpl = loader.load(path)
14 except TemplateNotFound:
15 start_response(404, [])
16 return ['Not found']
17 stream = tmpl.generate(**data)
18 start_resonse(200, [('Content-Type', 'text/html; charset=utf-8')])
19 return stream.render('html', doctype='html-strict')
20

Download Raw Source

Comments

  1. cmlenz says:

    15 July 2008
    21:39

    That last line should be: "return [stream.render('html', doctype='html-strict')]"
  2. cmlenz says:

    15 July 2008
    21:43

    And add a final "except" clause of course :P