如何在 Mako 模板中使用字典?
每当我将复杂的数据结构传递给 Mako 时,就很难对其进行迭代。例如,我传递了列表的字典的字典,要在 Mako 中访问它,我必须执行以下操作:
% for item in dict1['dict2']['list']: ... %endfor
我想知道 Mako 是否有某种机制可以替代 []
用法,用简单的 .
访问字典元素?
然后我可以将上面的行写为:
% for item in dict1.dict2.list: ... %endfor
哪个更好,不是吗?
谢谢,博达·西多。
Whenever I pass a complicated data structure to Mako, it's hard to iterate it. For example, I pass a dict of dict of list, and to access it in Mako, I have to do something like:
% for item in dict1['dict2']['list']: ... %endfor
I am wondering if Mako has some mechanism that could replace []
usage to access dictionary elements with simple .
?
Then I could write the line above as:
% for item in dict1.dict2.list: ... %endfor
Which is much nicer, isn't it?
Thanks, Boda Cydo.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Łukasz' 示例的简化:
另请参阅:http://code.activestate.com/recipes/52308-the-simple-but-handy-collector-of-a-bunch-of-named/
Simplification of Łukasz' example:
See also: http://code.activestate.com/recipes/52308-the-simple-but-handy-collector-of-a-bunch-of-named/
在将 dict1 传递给 Mako 模板之前,将其传递给
to_bunch
函数。不幸的是 Mako 没有提供任何钩子来自动执行此操作。Pass dict1 to
to_bunch
function before passing it to Mako template. Unfortunately Mako doesn't provide any hooks to do this automatically.