使用Mako作为表格数据的动态列

时间:2021-11-28 20:18:21

The mako documentation seems to hint a being able to have dynamic, optional layout just before "Using Blocks" but I am having a difficult time noodling it out.cle

mako文档似乎提示在“使用块”之前能够拥有动态的可选布局,但是我很难将其外推出来。

people = dict_cursor.fetchall()
all_possible_cols = ('Name','Birth','Gender', 'Address','City','State', ...]
user_cols = user_wants(user_id) # ('State','Gender','Name')

template.render(**locals())

How do I dynamically call the defs in the template to render the columns?

如何动态调用模板中的defs来呈现列?

mako template

% for person in people:
<tr>
  % for col in user_cols:
    How do I dynamically call the def/block?
  % endfor
</tr>
% endfor

<%def name="Name()">    ... </%def>
<%def name="Birth()">   ... </%def>
<%def name="Gender()">  ... </%def>
<%def name="Address()"> ... </%def>
<%def name="City()">    ... </%def>
<%def name="State()">   ... </%def>

So one user could want ('Name','Birth','Gender'), another ('Gender','State', 'Birth'), and a third could want them all in a different order. How can I cleanly support this functionality in mako templates?

因此,一个用户可能想要('姓名','出生','性别'),另一个用户('性别','州','出生'),而第三个用户可能想要它们以不同的顺序。如何在mako模板中干净地支持此功能?

1 个解决方案

#1


0  

If I understand your question, something like this should work:

如果我理解你的问题,这样的事情应该有效:

<tr>
  % for col in user_cols:
    ${getattr(self, col)()}
  % endfor
</tr>

#1


0  

If I understand your question, something like this should work:

如果我理解你的问题,这样的事情应该有效:

<tr>
  % for col in user_cols:
    ${getattr(self, col)()}
  % endfor
</tr>