Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Reserved variables in the templates #768

Closed
paulocheque opened this Issue · 2 comments

2 participants

@paulocheque

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

@ajdavis
Owner

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

@paulocheque

Thanks for your suggestion!

@bdarnell bdarnell referenced this issue from a commit
@bdarnell bdarnell Prefix all "reserved" names used by the template system with _tt_
to avoid collisions with user-defined names.

Inspired by #768.
1a1b049
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.