Skip to main content

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:
    @template('template_name', 'some arg', some='keyword arg')
    # pass your additional parameters here
    def handler():

def renderer(template_name, context, *args, **kwargs):
    # and do something with the parameters here

Developed under Python3.4 and tested against Python2.7, Python3.4 and pypy.


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:
    def index(self):
        return {'name': 'Lorem', 'lastname': 'Ipsum'}

    def page(self):
        return {'name': 'Lorem', 'lastname': 'Ipsum'}

    @template('page_with_args', 'some', 'arg')
    def page_with_args(self):
        return {'name': 'Lorem', 'lastname': 'Ipsum'}

    @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

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for lribeiro.cherrypy.templating, version 1.0.1
Filename, size File type Python version Upload date Hashes
Filename, size lribeiro.cherrypy.templating-1.0.1-py2.py3-none-any.whl (5.9 kB) File type Wheel Python version py2.py3 Upload date Hashes View
Filename, size lribeiro.cherrypy.templating-1.0.1.tar.gz (3.1 kB) File type Source Python version None Upload date Hashes View

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page