基本上,我有一个冒号定界变量被传入我的模板中,我想循环显示每个单独的元素。在django-templates中将一个字符串拆分成一个列表
我希望能够提取每个这些名称,然后在我的模板中对它们执行逻辑。
如果我可以使用Python,我会做这样的事情(注:FILE_TYPE是变量我有我的模板):
file_type = "PDF:DOC:XLS"
for tipe in file_type.split(":"):
if tipe == "PDF":
print "<img src='pdf'/>"
elif tipe == "DOC":
print "<img src='doc'/>"
elif tipe == "XLS":
print "<img src='xls'/>"
else:
print "<img src='unrecognized'/>"
所以基本上,有没有Django的模板分割功能或有任何人都会想出一个狡猾的方式来做到这一点?我一直无法找到任何东西。谢谢!
你能注册一个过滤器并在过滤器中调用split吗?看到http://djangosnippets.org/snippets/211/也http://djangosnippets.org/snippets/889/ – KevinDTimm 2010-10-28 16:21:53
谢谢凯文。这将是我的第一个自定义标签,我遇到了一些困难,尤其是考虑到我已经投入了一个项目来维护。 – Chris 2010-10-28 18:38:10