Template rendering tool for cherrypy
Project description
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.
Renderer parameters
You can optionally pass parameters to your renderer function by just passing additional arguments to the @template decorator
class Controller:
@cherrypy.expose
@template('template_name', 'some arg', some='keyword arg')
# pass your additional parameters here
def handler():
pass
def renderer(template_name, context, *args, **kwargs):
# and do something with the parameters here
pass
Developed under Python3.4 and tested against Python2.7, Python3.4 and pypy.
Usage:
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'}
@cherrypy.expose
@template('page_with_args', 'some', 'arg')
def page_with_args(self):
return {'name': 'Lorem', 'lastname': 'Ipsum'}
@cherrypy.expose
@template('page_with_args', some='keyword arg')
def page_with_args(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
Hashes for lribeiro.cherrypy.templating-1.0.1.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 501289c5654f9363eefd1a3cb58a0787427aa0274e0e604a3e8ef4292157a966 |
|
MD5 | b5a98dfb1523deb34660687274589412 |
|
BLAKE2b-256 | c87a9eca03386960c9c12c1382ec69e6ab9a3251d905a526b9beddbea34f79e1 |
Hashes for lribeiro.cherrypy.templating-1.0.1-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2d5880c04947a5ba2429874091fdc5a2c2df24288a866daf53725613901e8e08 |
|
MD5 | 4b38376b7c1dc5b7d8b65919d646719d |
|
BLAKE2b-256 | 48f2054718478db13a8733dc271a898683380d48675ce67a44404c77524404a6 |