## Metadata of each macro are stored in a new attribute
## of 'parser' class. That way we can access it later
## in the template when processing 'usemacro' tags.
try:
## Only try to access it to eventually trigger an exception
parser._macros
exceptAttributeError:
parser._macros={}
classDefineMacroNode(template.Node):
def__init__(self,name,nodelist,args):
self.name=name
self.nodelist=nodelist
self.args=args
defrender(self,context):
## empty string - {% macro %} tag does no output
return''
@register.tag(name="macro")
defdo_macro(parser,token):
try:
args=token.split_contents()
tag_name,macro_name,args=args[0],args[1],args[2:]
exceptIndexError:
raisetemplate.TemplateSyntaxError,"'%s' tag requires at least one argument (macro name)"%token.contents.split()[0]
# TODO: check that 'args' are all simple strings ([a-zA-Z0-9_]+)
r_valid_arg_name=re.compile(r'^[a-zA-Z0-9_]+$')
forarginargs:
ifnotr_valid_arg_name.match(arg):
raisetemplate.TemplateSyntaxError,"Argument '%s' to macro '%s' contains illegal characters. Only alphanumeric characters and '_' are allowed."%(arg,macro_name)
nodelist=parser.parse(('endmacro',))
parser.delete_first_token()
## Metadata of each macro are stored in a new attribute
## of 'parser' class. That way we can access it later
## in the template when processing 'usemacro' tags.