Handling kwargs in templates

10 views
Skip to first unread message

Gavin M. Roy

unread,
Dec 22, 2009, 5:58:06 PM12/22/09
to Tornado Web Server
I am looking for the "right" way to handle kwargs in a template.  I would like to pass in different arguments, such as a variable called error_message and have different behaviors based upon the existence of these variables.

Given the call:
self.render('templates/apps/home.html');

or

self.render('templates/apps/home.html', message='Danger, Will Robinson!');
I'm currently handling this with:
{% try %}
% if message %}
<p class="error">{{ message }}</p>
{% end %}
{% except %}
{% end %}
But I think there should be a more straight forward way to handle this such as finding the right object to check has_key() against.

Any suggestions?

Thanks,

Gavin

David P. Novakovic

unread,
Dec 22, 2009, 6:45:49 PM12/22/09
to python-...@googlegroups.com
The way we do it... not the absolute easiest way to work with, but it's ok.

in our base handler init we create self.context = {} - and prepopulate it with variables you an reasonably expect to be in every template, like self.context['messages'] = '' etc

Then through the handler we just keep adding stuff to self.context. Finally when we render the template we render it with self.render('templatename', **self.context)

David

Chris Tan

unread,
Dec 22, 2009, 8:27:42 PM12/22/09
to Tornado Web Server
One option would be to pass message=None to the render method.

You also could this in the template:
{% if 'message' in globals() and message %}
<stuff>
{% end %}

Chris Tan

unread,
Dec 22, 2009, 8:30:45 PM12/22/09
to Tornado Web Server
That's an interesting approach. I've been trying something like this
with some success. It's not pretty, but it works :)

def render_string(self, template, **kwargs):
return super(BaseHandler, self).render_string(
template, helpers=helpers, **kwargs)


On Dec 22, 3:45 pm, "David P. Novakovic" <davidnovako...@gmail.com>
wrote:

Gavin M. Roy

unread,
Dec 22, 2009, 10:54:33 PM12/22/09
to python-...@googlegroups.com
I'd rather not have to pass in message (even as None) when I don't need to, thus the crux of the question.

 {% if 'message' in globals() and message %}
      <stuff>
 {% end %}

Should do what I wanted, it was the globals() that was getting me... I was trying to find what dictionary it would be contained in.

Thanks!

Gavin

Gavin M. Roy

unread,
Dec 22, 2009, 10:55:23 PM12/22/09
to python-...@googlegroups.com
Thanks, I had thought of doing it this way as well, but I figured there had to be a more basic way of handling it that I wouldn't have to code around to make happen.

Creotiv

unread,
Dec 23, 2009, 3:57:56 AM12/23/09
to Tornado Web Server
something like that:

class contextObject():

data = {}

def __getattr__(self, name):
return self.data[name]

def __setattr__(self, name, val):
self.data[name] = val

def __setitem__(self,name,val):
self.data[name] = val

def __getitem__(self, name):
return self.data[name]

def get(self):
return self.data

tplContext = contextObject()

class MyController(request.RequestHandler):
def _render(self, template):
"""
Render function for Jinja2
"""
env = self.application.env
self.set_header("Content-Type", "text/html; charset=UTF-8")
tpl = env.get_template(template)
self.write(tpl.render(**tplContext.get()))

def index(self):
self.set_header("Content-Type", "text/html; charset=UTF-8")
tplContext.type = '13a'
tplContext['test'] = 'test'
self._render('test.html')
self.finish()

Japhy Bartlett

unread,
Dec 23, 2009, 12:31:45 PM12/23/09
to python-...@googlegroups.com
seems like you could use a defaultdict( lambda: None) somewhere clever

Creotiv

unread,
Dec 23, 2009, 3:05:52 PM12/23/09
to Tornado Web Server
I started use Python just 2 weeks ago.. so i not very good know its
hacks.. just using standart algorithms and logic that in the most
languages is the same.
Reply all
Reply to author
Forward
0 new messages