1

我有大量消息,特定于单个页面,我可能会在晚上展示其中的任何组合。为了简化添加新消息(大量在短期内会变得更大),并防止出现一个巨大的 if/elif 块,每个条目都有一个条目,我想要一种方法来包含所有相关模板,而无需每次添加新消息时都必须更新模板。我的想法是让表示消息的 dict 包含它的模板。这是我这样做的尝试:

<div class="item-content" id="results_content">
  <ul class="unstyled">
     %for msg in c.page_messages:
       <%include file="${msg.get('template_path')}" args="message=msg"/>
     %endfor
  </ul>
</div>

这会在 %include 标记处产生错误:

TypeError: 'NoneType' object has no attribute '__getitem__'
  1. 是否可以确认我是否可以使用包含标签的模板变量,如果我希望这可以工作,我似乎找不到任何具体说明任何一种方式的东西。
  2. 如果这不是可行的方法,是否有另一种方法可以完成同样的事情?ie - 可以添加一条新消息,而无需修改这个引入每个消息模板的模板代码。
4

1 回答 1

4

看起来您可以按照您描述的方式在 include 标记中使用变量。从文档

All tags have a set of attributes which are defined for each tag. Some of these attributes
are required. Also, many attributes support evaluation, meaning you can embed an     
expression (using ${}) inside the attribute text:

<%include file="/foo/bar/${myfile}.txt"/>

我的猜测是 c.page_messages 在这一点上不是一个可迭代的字典。

于 2013-10-16T18:08:20.210 回答