This package contains utility classes and functions for rendering Genshi templates from pesto applications.
For more examples of usage, please see the source code and tests.
Decorators and utility functions to integrate pesto handlers with the Genshi templating system.
A GenshiRender instance provides decorators and functions for rendering Genshi templates to pesto Response objects or to strings.
There are methods to render using both Genshi’s MarkupTemplate or TextTemplate, and to return the result as a string, a genshi stream, or a response object.
This example shows the class being used as a decorator. Calling myfunc will return a pesto.response.Response object to be returned with the contents of ‘page.html’ rendered with the variables provided in the return value of myfunc:
>>> render = GenshiRender(TemplateLoader(['path/to/templates/']))
>>> @render('page.html')
... def myfunc():
... return {'x': 'foo', 'y': 'bar'}
...
Instead of rendering a response object directly it’s also possible to simply return the rendered value as string:
>>> @render.as_string('page.html')
... def myfunc():
... return {'x': 'foo', 'y': 'bar'}
...
For rapid prototyping, it’s also possible to render function docstrings as Genshi templates:
>>> @render.docstring()
... def myfunc():
... '''
... <html>
... $foo
... </html>
... '''
... return {'foo': 'bar'}
Each of the rendering methods can also be called as a regular (non-decorator) method, for example:
>>> render.as_string('page.html', {'foo': 'bar'})
Return a response object for the rendered template:
>>> render = GenshiRender(TemplateLoader(['.']))
>>> response = render.as_response('my_template.html', {'foo': 'bar'})
Can also be used as a decorator. The decorated function will merge the original function’s return value (a dict) with the specified template:
>>> from pestotools.genshi import GenshiRender
>>> from pesto import currentrequest
>>> render = GenshiRender(TemplateLoader(['.']))
>>>
>>> @render.as_response('my_template.html')
... def handler(request):
... return {'foo': 'bar'}
...
Return a Genshi stream for the rendered template
Return the string output of the rendered template. Can also work as a function decorator
Render the function’s docstring as a genshi template stream:
>>> render = GenshiRender()
>>> @render.docstring()
... def myhandler(request):
... '''
... <html>
... $foo
... </html>
... '''
... return {'foo': 'bar'}
Render the function’s docstring as a genshi template stream:
>>> render = GenshiRender()
>>> @render.docstring_as_string()
... def myfunc():
... '''
... <html>
... $foo
... </html>
... '''
... return {'foo': 'bar'}
Same as as_string but use a Genshi TextTemplate and text serializer
Same as as_stream but use a Genshi TextTemplate
Same as as_string but use a Genshi TextTemplate and text serializer
Take a function emitting a genshi stream and apply an XPATH select to it to filter out all but the selected elements, eg:
>>> render = GenshiRender()
>>> @select("//p[@id='content']")
... @render.docstring()
... def myfunc():
... '''
... <html>
... <h1>Welcome to $foo!</h1>
... <p id="content">$foo<p>
... </html>
... '''
... return {'foo': 'bar'}
...
>>> myfunc().content
['<p id="content">bar</p>']
Apply the genshi HTMLFormFiller filter to a genshi stream, populating form fields from the request object or other specified defaults.
Return a WSGI application serving genshi templated HTML pages. Example:
>>> from wsgiref.simple_server import make_server
>>> render = GenshiRender(TemplateLoader(['path/to/document/root']))
>>> app = GenshiApp(render)
>>> print "Serving on port 8000..."
>>> make_server('', 8000, app).serve_forever()
Return a WSGI app serving Genshi templated pages from a filesystem directory.