Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tornado template rendering fail. #89

Open
darkdarkfruit opened this issue Oct 23, 2012 · 1 comment
Open

tornado template rendering fail. #89

darkdarkfruit opened this issue Oct 23, 2012 · 1 comment

Comments

@darkdarkfruit
Copy link

Tornado loader could not render template and app exit when I tried demo_tornado.py. ( easy regenerated)

Finally I located problems here.

tornado template loader do not have method "get_template", and tornado template do not have method "render"

https://github.com/j2labs/brubeck/blob/master/brubeck/request_handling.py#L702

def render_template(template_file, **context):
                    """Renders template using provided template environment.
                    """
                    if hasattr(self, 'template_env'):
                        t_env = self.template_env

                        # tornado template loader do not have method "get_template", and tornado template do not have method "render"
                        template = t_env.get_template(template_file)
                        body = template.render(**context or {})
                    return body

hardcode patch for tornado (can work for tornado but not beautiful):

def render_template(template_file, **context):
                    """Renders template using provided template environment.
                    """
                    if hasattr(self, 'template_env'):
                        t_env = self.template_env

                        # !!! just for demostration, here I omit the processing code of other templates: jinja2, mako, .
                        # temporary patch for tornado
                        template = t_env.load(template_file)
                        body = template.generate(**context or {})

                    return body

May be a better way is to adapt tornado loader in
function "load_tornado_env" of file "templating.py"

Any smarter patch suggest?

@darkdarkfruit
Copy link
Author

find a better way to patch in templating.py,

class TornadoRendering(WebMessageHandler):
    """TornadoRendering is a mixin for for loading a Tornado rendering
    environment.

    Follows usual convention: 200 => success and 500 => failure

    The unusual convention of an underscore in front of a variable is used
    to avoid conflict with **context. '_status_code', for now, is a reserved
    word.
    """
    def render_template(self, template_file,
                        _status_code=WebMessageHandler._SUCCESS_CODE,
                        **context):
        """Renders payload as a tornado template
        """
        #### patch starts here ########
        if self.application.template_env:
            template = self.application.template_env.load(template_file)
            body = template.generate(**context or {})
        else:
            body = ''

        # body = self.application.render_template(template_file, **context or {})
         #### patch ends here ########

        self.set_body(body, status_code=_status_code)
        return self.render()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant