/examples/web/makotemplates.py

https://bitbucket.org/prologic/circuits/ · Python · 42 lines · 28 code · 13 blank · 1 comment · 1 complexity · d7c9627e261c185671e41c7178c34a62 MD5 · raw file

  1. #!/usr/bin/env python
  2. import os
  3. import mako
  4. from mako.lookup import TemplateLookup
  5. from circuits.web import Server, Controller, Static
  6. DEFAULTS = {}
  7. templates = TemplateLookup(
  8. directories=[os.path.join(os.path.dirname(__file__), "tpl")],
  9. module_directory="/tmp",
  10. output_encoding="utf-8"
  11. )
  12. def render(name, **d):
  13. try:
  14. d.update(DEFAULTS)
  15. tpl = templates.get_template(name)
  16. return tpl.render(**d)
  17. except:
  18. return mako.exceptions.html_error_template().render()
  19. class Root(Controller):
  20. tpl = "index.html"
  21. def index(self):
  22. return render(self.tpl)
  23. def submit(self, firstName, lastName):
  24. msg = "Thank you %s %s" % (firstName, lastName)
  25. return render(self.tpl, message=msg)
  26. app = Server(("0.0.0.0", 8000))
  27. Static().register(app)
  28. Root().register(app)
  29. app.run()