本站首页    管理页面    写新日志    退出


«October 2025»
1234
567891011
12131415161718
19202122232425
262728293031


公告
 本博客在此声明所有文章均为转摘,只做资料收集使用。

我的分类(专题)

日志更新

最新评论

留言板

链接

Blog信息
blog名称:
日志总数:1304
评论数量:2242
留言数量:5
访问次数:7626751
建立时间:2006年5月29日




[Django]Pyif Tag
软件技术

lhwork 发表于 2007/1/31 18:13:42

Code ¶from django import template from django.utils.translation import gettext_lazy as _ register = template.Library() class PyIfNode(template.Node): def __init__(self, nodeslist): self.nodeslist = nodeslist def __repr__(self): return "<PyIf node>" def render(self, context): for e, nodes in self.nodeslist: clist = list(context) clist.reverse() d = {} d['_'] = _ for c in clist: d.update(c) v = eval(e, d) if v: return nodes.render(context) return '' def do_pyif(parser, token): nodeslist = [] while 1: v = token.contents.split(None, 1) if v[0] == 'endif': break if v[0] in ('pyif', 'elif'): if len(v) < 2: raise template.TemplateSyntaxError, "'pyif' statement requires at least one argument" if len(v) == 2: tagname, arg = v else: tagname, arg = v[0], 'True' nodes = parser.parse(('else', 'endif', 'elif')) nodeslist.append((arg, nodes)) token = parser.next_token() # parser.delete_first_token() return PyIfNode(nodeslist) do_pyif = register.tag("pyif", do_pyif) How to use it ¶ {% pyif i == 1 %} <p>i=1</p> {% elif i == 3 %} <p>i=3</p> {% else %} <p>other</p> {% endif %} Warning: For now, django don't support elif, so you can use it. And I'v submit a patch about to fix it ( #3090 ). If the patch is accepted, you can use elif tag.


阅读全文(1534) | 回复(0) | 编辑 | 精华
 



发表评论:
昵称:
密码:
主页:
标题:
验证码:  (不区分大小写,请仔细填写,输错需重写评论内容!)



站点首页 | 联系我们 | 博客注册 | 博客登陆

Sponsored By W3CHINA
W3CHINA Blog 0.8 Processed in 0.047 second(s), page refreshed 144784521 times.
《全国人大常委会关于维护互联网安全的决定》  《计算机信息网络国际联网安全保护管理办法》
苏ICP备05006046号