开发者

Split a string into a list in django-templates

Basically, I have a colon delimited variable being passed into my template that I would like to loop through to display each individual element.

I would like to be able to extract each of these names and then perform logic on them within my template.

If I could use python, I would do something like (note: file_type is the variable I have in my template):

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='unrecogn开发者_StackOverflowized'/>"

So basically, is there a split function in django-templates or has anyone come up with a crafty way to do this? I haven't been able to find anything. Thanks!


This is a job for a custom template tag or filter. If you ever find yourself thinking "If I could use Python here", that's the sign you need a custom tag/filter. You can more or less use the code you give above, just return the value instead of printing it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜