【问题标题】:Dynamic columns using Mako for tabular data使用 Mako 处理表格数据的动态列
【发布时间】:2011-08-29 20:29:49
【问题描述】:

mako 文档似乎暗示了在“使用块”之前能够拥有动态的可选布局,但我很难将它弄清楚。cle

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())

如何动态调用模板中的defs来渲染列?

魔子模板

% 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>

因此,一个用户可能想要 ('Name','Birth','Gender'),另一个用户 ('Gender','State', 'Birth'),而第三个用户可能希望它们以不同的顺序排列。如何在 mako 模板中完全支持此功能?

【问题讨论】:

    标签: python templates dynamic mako


    【解决方案1】:

    如果我理解你的问题,这样的事情应该可以工作:

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

    【讨论】:

      猜你喜欢
      • 2013-01-29
      • 2013-10-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-26
      • 2017-01-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多