Package madrona :: Package common :: Package templatetags :: Module set_var
[hide private]

Source Code for Module madrona.common.templatetags.set_var

 1  from django import template 
 2   
 3  register = template.Library() 
 4   
5 -class SetVarNode(template.Node):
6
7 - def __init__(self, var_name, var_value):
8 self.var_name = var_name 9 self.var_value = var_value
10
11 - def render(self, context):
12 try: 13 value = template.Variable(self.var_value).resolve(context) 14 except template.VariableDoesNotExist: 15 value = "" 16 context[self.var_name] = value 17 return u""
18
19 -def set_var(parser, token):
20 """ 21 {% set <var_name> = <var_value> %} 22 """ 23 parts = token.split_contents() 24 if len(parts) < 4: 25 raise template.TemplateSyntaxError("'set' tag must be of the form: {% set <var_name> = <var_value> %}") 26 return SetVarNode(parts[1], parts[3])
27 28 register.tag('set', set_var) 29