gpt4 book ai didi

python - 变色龙模板循环

转载 作者:行者123 更新时间:2023-12-01 05:55:06 25 4
gpt4 key购买 nike

我尝试使用变色龙创建模板。这是代码片段。

调用模块runtemp.py:

delete_list=[]
delete={'Name':'aaa','Sirname':'bbb','Friends':['ccc','ddd','eee']}
delete_list.append(delete)
templates = PageTemplateLoader(os.path.join(path, "templates"))
template = templates["delete_user.pt"]
print template(tdelete_list=delete_list)

模板文件delete_list.pt:

 <?xml version="1.0" encoding="UTF-8"?>
<Delete>
<DeleteRequest>

<DeleteItems tal:repeat="deletions tdelete_list">

<Deleteuser tal:repeat="delete repeat.deletions" >

<Name tal:content="repeat.delete.Name"></Name>
<Sirname tal:content="repeat.delete.Sirname"></Sirname>
<Friends>
<Friend tal:repeat="friend repeat.delete.Friends">
<Value tal:content="friend"></Value>
</Friend>
</Friends>

</Deleteuser>

<DeleteItems>

</DeleteRequest>

</Delete>

我得到的输出:

 <Delete>
<DeleteRequest>

<DeleteItems>



</DeleteItems>
</DeleteRequest>
</Delete>

我的问题是中间标签没有被打印;怎么了?

最佳答案

线路<DeleteItems tal:repeat="deletions tdelete_list">表示循环tdelete_list并将每个元素放入变量 deletions 中.

因此,您的内部循环只需要循环 deletions ; repeat.此处使用前缀:

<Deleteuser tal:repeat="delete deletions" >

<Name tal:content="delete.Name"></Name>
<Sirname tal:content="delete.Sirname"></Sirname>
<Friends>
<Friend tal:repeat="friend delete.Friends">
<Value tal:content="friend"></Value>
</Friend>
</Friends>

</Deleteuser>

repeat.deletions变量实际上仅用于存储循环元数据;当前计数,firstlast标志,oddeven旗帜等

关于python - 变色龙模板循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12972592/

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