Created
July 23, 2013 00:09
-
-
Save eneldoserrata/6058794 to your computer and use it in GitHub Desktop.
cherrypy mako plugin
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 -*- | |
import cherrypy | |
from cherrypy.process.plugins import SimplePlugin | |
from mako import exceptions | |
from mako.lookup import TemplateLookup | |
# todo-eneldo Documentar | |
# todo-eneldo capturar exceptions | |
# todo-eneldo crear test units | |
""" | |
debes de pasarle a cherrypy config estos parametrod para decirle donde se encuantran los template: | |
TEMPLATE_DIRECTORIES: [os.path.join(marcos.BASE_DIR, "templates"),] | |
TEMPLATE_MODULE_DIRECTORY: os.path.join(marcos.BASE_DIR, "cache/mako") | |
luego para inicializarlo unitilisas la funcion makoSetup(cherrypy.engine) | |
para utilizarlo colocas el decorator @cherrypy.render(template="mytemplate") sobre la funcion que expones | |
""" | |
class MakoPlugin(SimplePlugin): | |
def __init__(self, engine, directories=None, module_directory=None, collection_size=50, encoding="utf-8"): | |
SimplePlugin.__init__(self, engine) | |
if directories is None: | |
self.directories = cherrypy.config.get("TEMPLATE_DIRECTORIES", None) | |
else: | |
self.directories = directories | |
if module_directory is None: | |
self.module_directory = cherrypy.config.get("TEMPLATE_MODULE_DIRECTORY", None) | |
else: | |
self.module_directory = module_directory | |
self.encoding = encoding | |
self.collection_size = collection_size | |
self.lookup = None | |
self.subscribe() | |
def start(self): | |
self.lookup = TemplateLookup(directories=self.directories, module_directory=self.module_directory, | |
input_encoding=self.encoding, | |
output_encoding=self.encoding, | |
collection_size=self.collection_size) | |
self.bus.subscribe("lookup-templates", self.render_template) | |
def stop(self): | |
self.bus.unsubscribe("lookup-templates", self.render_template) | |
self.lookup = None | |
def render_template(self, template): | |
return self.lookup.get_template(template) | |
class MakoTool(cherrypy.Tool): | |
def __init__(self): | |
cherrypy.Tool.__init__(self, "before_finalize", | |
self._render, | |
priority=30) | |
def _render(self, template=None, debug=False): | |
data = cherrypy.response.body or {} | |
template = cherrypy.engine.publish("lookup-templates", template).pop() | |
if template and isinstance(data, dict): | |
if debug: | |
try: | |
cherrypy.response.body = template.render(**data) | |
except: | |
cherrypy.response.body = exceptions.html_error_template().render() | |
else: | |
cherrypy.response.body = template.render(**data) | |
# This tool register when module import | |
cherrypy.tools.render = MakoTool() | |
def makoSetup(engine, directories=None, module_directory=None, encoding="utf-8"): | |
MakoPlugin(engine=engine, directories=directories, module_directory=module_directory, encoding=encoding) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi, eneldo sir...
Can you help me for building this app? I am stuck, I want to render the json data to the view and make view look good. But i am not able to render the JSON response.