gpt4 book ai didi

python - 动态设置 Mako 模板以包含另一个模板

转载 作者:太空宇宙 更新时间:2023-11-04 10:43:07 25 4
gpt4 key购买 nike

我有一大堆消息,特定于单个页面,我可能会在晚上显示它们的任意组合。为了简化添加新消息(大量消息在短期内会变得更大),并防止有一个巨大的 if/elif block ,每个 block 都有一个条目,我想要一种方法来包含所有相关模板而不每次添加新消息时都必须更新模板。我的想法是让代表消息的 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. 如果这行不通,有没有其他方法可以完成同样的事情?即 - 可以添加新消息,而无需修改此模板代码以拉入每个消息模板。

最佳答案

看起来您可以按照您描述的方式在 include 标记中使用变量。 From the docs :

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 此时不是字典的可迭代对象。

关于python - 动态设置 Mako 模板以包含另一个模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19303817/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com