I'v written a expression tag, it can be used to calculate python expression and save the result to a template variable. I think it maybe some useful for someone.
-------------------------------utiltags.py----------------------------------- from django import template import re register = template.Library() class ExprNode(template.Node): def __init__(self, expr_string, var_name): self.expr_string = expr_string self.var_name = var_name def render(self, context): clist = list(context) clist.reverse() d = {} for c in clist: d.update(c) context[self.var_name] = eval(self.expr_string, d) return '' def do_expr(parser, token): try: # Splitting by None == splitting by spaces. tag_name, arg = token.contents.split(None, 1) except ValueError: raise template.TemplateSyntaxError, "%r tag requires arguments" % token.contents[0] m = re.search(r'(.*?) as (\w+)', arg) if not m: raise template.TemplateSyntaxError, "%r tag had invalid arguments" % tag_name expr_string, var_name = m.groups() return ExprNode(expr_string, var_name) do_expr = register.tag('expr', do_expr) -----------------------------How to use it--------------------------------- {% load utiltags %} {% expr 1 as a %} {% expr 2 as b %} {% expr a+b as c %} {{ a }}+{{ b }}={{ c }} -- I like python! My Blog: http://www.donews.net/limodou NewEdit Maillist: http://groups.google.com/group/NewEdit --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users -~----------~----~----~----~------~----~------~--~---