Reputation: 62624
I have a variable myvariable that I want to use in a Mako template. I want to be able to somehow check its type before doing anything with it. What is the syntax to check that sort of information? I know python has typeof and instanceof, but is there some equivalent in Mako or how would you do it?
Pseudocode below:
% if myvariable == 'list':
// Iterate Throuh the List
%for item in myvariable:
${myvariable[item]}
%endfor
%elif variabletype == 'int':
// ${myvariable}
%elif myvariable == 'dict':
// Do something here
Upvotes: 2
Views: 677
Reputation: 473863
You can use isinstance()
:
>>> from mako.template import Template
>>> print Template("${isinstance(a, int)}").render(a=1)
True
>>> print Template("${isinstance(a, list)}").render(a=[1,2,3,4])
True
UPD. Here's the usage inside if/else/endif:
from mako.template import Template
t = Template("""
% if isinstance(a, int):
I'm an int
% else:
I'm a list
% endif
""")
print t.render(a=1) # prints "I'm an int"
print t.render(a=[1,2,3,4]) # prints "I'm a list"
Upvotes: 2