Using python variables in Mako control structures

1,116 views
Skip to first unread message

John Martin

unread,
Nov 6, 2009, 6:05:41 PM11/6/09
to Mako Templates for Python
Hi,

I'm a complete newbie to Mako and to templating in general. I'd like
to use Mako to create a report
where the length of certain elements in the report isn't known until
runtime.

To that end, I'd like to pass a list from my python code into a Mako
template, and have the
template loop over the elements in the list.

My python code is:

from mako.template import Template
my_dict = {}
my_dict['names'] = ['alice', 'bob']
t = Template(filename=fn)
print t.render(**my_dict)

where fn is the name of my template which contains:

% for name in ${names}:
${name}
% endfor

Running the code gives a mako.exceptions.SyntaxException at the first
line of the template.

I must be missing something very obvious here - why doesn't the
"names" list get substituted into ${names} in the for loop?

Thanks,
John


Bobby Impollonia

unread,
Nov 6, 2009, 6:24:30 PM11/6/09
to mako-d...@googlegroups.com
It's just "for name in names:". The control structures are python so
there is no curly braces and dollar signs.
Reply all
Reply to author
Forward
0 new messages