Created
October 1, 2014 07:21
-
-
Save stavinsky/12234ba03be54308bbb4 to your computer and use it in GitHub Desktop.
CherryPy jinja2 decorator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
""" | |
using: | |
class Root(BaseController): | |
''' Cherrypy Root class | |
''' | |
@jinja_template('main_page.html') | |
def index(self, lang=''): | |
user = 'user' | |
title = 'title' | |
return dict( | |
user=user, | |
title=title | |
) | |
""" | |
from jinja2 import Environment, FileSystemLoader | |
env = Environment(loader=FileSystemLoader('%s/templates' % os.environ['OPENSHIFT_REPO_DIR'])) # put your own templates dir | |
def jinja_template(template_name): | |
def decorator(func): | |
def wrapper(*args, **kwargs): | |
tmpl = env.get_template(template_name) | |
return tmpl.render(**func(*args, **kwargs)) | |
return wrapper | |
return decorator |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment