-
-
Save lpirir/6a4c29a9e9e98400c7b9 to your computer and use it in GitHub Desktop.
Revisions
-
jhargis revised this gist
Sep 23, 2014 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -22,7 +22,7 @@ def stop(self): """ Called when the engine stops. """ self.bus.log('Freeing up Jinja2 resources') self.bus.unsubscribe("lookup-template", self.get_template) self.env = None -
jhargis created this gist
Sep 23, 2014 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,32 @@ # -*- coding: utf-8 -*- # http://docs.cherrypy.org/en/latest/advanced.html?highlight=jinja2#html-templating-support import os.path import cherrypy class Root(object): @cherrypy.expose def index(self): return {'msg': 'Hello world!'} if __name__ == '__main__': cherrypy.config.update({'server.socket_port': 8090}) # Register the Jinja2 plugin from jinja2 import Environment, FileSystemLoader from jinja2plugin import Jinja2TemplatePlugin env = Environment(loader=FileSystemLoader('.')) Jinja2TemplatePlugin(cherrypy.engine, env=env).subscribe() # Register the Jinja2 tool from jinja2tool import Jinja2Tool cherrypy.tools.template = Jinja2Tool() # We must disable the encode tool because it # transforms our dictionary into a list which # won't be consumed by the jinja2 tool cherrypy.quickstart(Root(), '', {'/': {'tools.template.on': True, 'tools.template.template': 'index.html', 'tools.encode.on': False}}) This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,10 @@ <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-15"> <title></title> </head> <body> {{msg}} </body> </html> This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,37 @@ # -*- coding: utf-8 -*- import cherrypy from cherrypy.process import plugins __all__ = ['Jinja2TemplatePlugin'] class Jinja2TemplatePlugin(plugins.SimplePlugin): """A WSPBus plugin that manages Jinja2 templates""" def __init__(self, bus, env): plugins.SimplePlugin.__init__(self, bus) self.env = env def start(self): """ Called when the engine starts. """ self.bus.log('Setting up Jinja2 resources') self.bus.subscribe("lookup-template", self.get_template) def stop(self): """ Called when the engine stops. """ self.bus.log('Freeing up Mako resources') self.bus.unsubscribe("lookup-template", self.get_template) self.env = None def get_template(self, name): """ Returns Jinja2's template by name. Used as follow: >>> template = cherrypy.engine.publish('lookup-template', 'index.html').pop() """ return self.env.get_template(name) This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,27 @@ # -*- coding: utf-8 -*- import cherrypy __all__ = ['Jinja2Tool'] class Jinja2Tool(cherrypy.Tool): def __init__(self): cherrypy.Tool.__init__(self, 'before_finalize', self._render, priority=10) def _render(self, template=None, debug=False): """ Applied once your page handler has been called. It looks up the template from the various template directories defined in the Jinja2 plugin then renders it with whatever dictionary the page handler returned. """ if cherrypy.response.status > 399: return # retrieve the data returned by the handler data = cherrypy.response.body or {} template = cherrypy.engine.publish("lookup-template", template).pop() if template and isinstance(data, dict): cherrypy.response.body = template.render(**data)