Template rendering tool for cherrypy
Project description
============================
lribeiro.cherrypy.templating
============================
Template rendering tool for cherrypy.
**It does not render any template**, so you must provide a renderer for your template system to do the actual rendering.
The renderer must be a callable accepting two arguments:
- The name of the template to be rendered
- A context object, which can be a ``dict`` or any other object that your template system can handle
The template name comes from the ``@template`` decorator argument and the context is the value return from the handler.
Usage:
------
.. sourcecode:: python
from os import path
import cherrypy
import pystache
from lribeiro.cherrypy.templating import template
def _renderer(template_name, context):
"""
Template renderer using Pystache
"""
search_dir = path.join(path.dirname(path.abspath(__file__)), 'templates')
renderer = pystache.Renderer(file_extension='html', search_dirs=search_dir)
template_str = renderer.load_template(template_name)
return renderer.render(template_str, context)
class Root:
@cherrypy.expose
@template('index')
def index(self):
return {'name': 'Lorem', 'lastname': 'Ipsum'}
@cherrypy.expose
@template('page')
def page(self):
return {'name': 'Lorem', 'lastname': 'Ipsum'}
config = {
'/': {
'templating.renderer': _renderer,
}
}
if __name__ == '__main__':
cherrypy.quickstart(Root(), '/', config)
lribeiro.cherrypy.templating
============================
Template rendering tool for cherrypy.
**It does not render any template**, so you must provide a renderer for your template system to do the actual rendering.
The renderer must be a callable accepting two arguments:
- The name of the template to be rendered
- A context object, which can be a ``dict`` or any other object that your template system can handle
The template name comes from the ``@template`` decorator argument and the context is the value return from the handler.
Usage:
------
.. sourcecode:: python
from os import path
import cherrypy
import pystache
from lribeiro.cherrypy.templating import template
def _renderer(template_name, context):
"""
Template renderer using Pystache
"""
search_dir = path.join(path.dirname(path.abspath(__file__)), 'templates')
renderer = pystache.Renderer(file_extension='html', search_dirs=search_dir)
template_str = renderer.load_template(template_name)
return renderer.render(template_str, context)
class Root:
@cherrypy.expose
@template('index')
def index(self):
return {'name': 'Lorem', 'lastname': 'Ipsum'}
@cherrypy.expose
@template('page')
def page(self):
return {'name': 'Lorem', 'lastname': 'Ipsum'}
config = {
'/': {
'templating.renderer': _renderer,
}
}
if __name__ == '__main__':
cherrypy.quickstart(Root(), '/', config)
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Close
Hashes for lribeiro.cherrypy.templating-1.0.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 44a327a1fc6521599f0944557cb7ea6beccbdc7429a03320b8209dd242a99537 |
|
MD5 | ee2d93394fdfcd839633dfad05f9add1 |
|
BLAKE2b-256 | 1f1e04a4ec833fb86d07644df390fbaac29b3162fd80a78f21a775383e9ab31f |
Close
Hashes for lribeiro.cherrypy.templating-1.0-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | f725df111591fd038c250d9e16284ea9798cd1c960b8f06d05e39358e3ea1add |
|
MD5 | 24c2f667a21e7b55af8d48c9e4fff77c |
|
BLAKE2b-256 | d71ac540b2604e260d0d0bb0f1c7a131b6c656fc8b05af13e0d7c10bf642d13b |