python - How to check instanceof in mako template? -
i have variable myvariable want use in mako template. want able somehow check type before doing it. syntax check sort of information? know python has typeof , instanceof, there equivalent in mako or how it?
pseudocode below:
% if myvariable == 'list': // iterate throuh list %for item in myvariable: ${myvariable[item]} %endfor %elif variabletype == 'int': // ${myvariable} %elif myvariable == 'dict': // here
you can use isinstance()
:
>>> 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 usage inside if/else/endif:
from mako.template import template t = template(""" % if isinstance(a, int): i'm int % else: i'm list % endif """) print t.render(a=1) # prints "i'm int" print t.render(a=[1,2,3,4]) # prints "i'm list"
Comments
Post a Comment