我想使用 adef
作为函数,并从if
块中调用它:
<%def name="check(foo)">
% if len(foo.things) == 0:
return False
% else:
% for thing in foo.things:
% if thing.status == 'active':
return True
% endif
% endfor
% endif
return False
</%def>
% if check(c.foo):
# render some content
% else:
# render some other content
% endif
不用说,这种语法不起作用。我不想只做一个表达式替换(并且只渲染 def 的输出),因为逻辑是一致的,但是渲染的内容因地而异。
有没有办法做到这一点?
编辑:
将逻辑包含在 def in 中<% %>
似乎是要走的路。