CherryPy example ================ This describes a short example of how to best use Templess in combination with `CherryPy`_, a simple but versatile web framework. The tips here can be applied to all systems, although CherryPy has the advantage of being able to serve generator-based functions (where the result of the generator is streamed to the client), which Templess provides hooks for. In systems that don't allow this, 't.generate(self.get_context())' will have to be replaced with something like 't.unicode(self.get_context())'. The code -------- Here's a simple example that uses this principle in an 'hello, world' fashion, in 'real life' situations obviously the context will be a lot larger, however, the 'index' method (that actually serves the data) can be quite similar. Contents of 'index.html' (placed in the CherryPy application directory):: </head> <body t:content="body" /> </html> Contents of 'hello.py' (the CherryPy application, serve with 'python hello.py'):: import cherrypy from templess.templess import template class HelloWorld(object): def index(self): t = template(open('index.html')) # this _returns_ the generator, which in turn yields chunks of # rendered template return t.generate(self.get_context()) index.exposed = True def get_context(self): return {'title': 'Hello, World!', 'body': 'Hello, World! example using template.generate()'} cherrypy.quickstart(HelloWorld()) Note that there was a bug in Templess 0.2's generate() method, this is fixed in the current version. .. _`CherryPy`: http://www.cherrypy.org