PageRenderTime 16ms CodeModel.GetById 12ms app.highlight 2ms RepoModel.GetById 1ms app.codeStats 0ms

/examples/web/makotemplates.py

https://bitbucket.org/prologic/circuits/
Python | 42 lines | 28 code | 13 blank | 1 comment | 0 complexity | d7c9627e261c185671e41c7178c34a62 MD5 | raw file
 1#!/usr/bin/env python
 2
 3import os
 4
 5import mako
 6from mako.lookup import TemplateLookup
 7
 8from circuits.web import Server, Controller, Static
 9
10DEFAULTS = {}
11
12templates = TemplateLookup(
13    directories=[os.path.join(os.path.dirname(__file__), "tpl")],
14    module_directory="/tmp",
15    output_encoding="utf-8"
16)
17
18
19def render(name, **d):
20    try:
21        d.update(DEFAULTS)
22        tpl = templates.get_template(name)
23        return tpl.render(**d)
24    except:
25        return mako.exceptions.html_error_template().render()
26
27
28class Root(Controller):
29
30    tpl = "index.html"
31
32    def index(self):
33        return render(self.tpl)
34
35    def submit(self, firstName, lastName):
36        msg = "Thank you %s %s" % (firstName, lastName)
37        return render(self.tpl, message=msg)
38
39app = Server(("0.0.0.0", 8000))
40Static().register(app)
41Root().register(app)
42app.run()