Reserved variables in the templates #768

Closed
paulocheque opened this Issue Apr 28, 2013 · 2 comments

Comments

Projects
None yet
2 participants

I am trying to override the render method in a Base RequestHandler, so I can add some variables to all templates. But I am having some strange errors for some variables, that I believe are reserved by Tornado. Am I correct? How can I get a list of these variables?

kwargs['xx'] = 42
return super(X, self).render(template_name, **kwargs)
{{ xx }} => prints 42

kwargs['authenticated'] = 42
return super(X, self).render(template_name, **kwargs)
{{ authenticated }} => error: undefined variable

kwargs['current_user'] = 42
return super(X, self).render(template_name, **kwargs)
{{ current_user }} => error: undefined variable

Thanks in advance

Contributor

ajdavis commented Apr 28, 2013

Works for me. server.py:

import tornado.ioloop
import tornado.web


class X(tornado.web.RequestHandler):
    def render(self, template_name, **kwargs):
        kwargs['current_user'] = 42
        return super(X, self).render(template_name, **kwargs)


class Y(X):
    def get(self):
        self.render('foo.html')

application = tornado.web.Application([(r"/", Y)])

if __name__ == "__main__":
    application.listen(8888)
    tornado.ioloop.IOLoop.instance().start()

foo.html:

{{ current_user }}

In any case, try, get_template_namespace instead of overriding render, it may be a little cleaner.

http://www.tornadoweb.org/en/stable/web.html#tornado.web.RequestHandler.get_template_namespace

Thanks for your suggestion!

bdarnell added a commit that referenced this issue Apr 28, 2013

Prefix all "reserved" names used by the template system with _tt_
to avoid collisions with user-defined names.

Inspired by #768.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment