将字符串拆分为django-templates中的列表

Chr*_*ris 1 django 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'/>"
Run Code Online (Sandbox Code Playgroud)

基本上,django-templates中是否存在拆分功能,或者有没有人想出一个狡猾的方法来做到这一点?我找不到任何东西.谢谢!

Dan*_*man 5

这是自定义模板标记或过滤器的作业.如果你发现自己在想"如果我可以在这里使用Python",那就是你需要一个自定义标签/过滤器的标志.您可以或多或少地使用上面给出的代码,只返回值而不是打印它.