1 from django import template
2 from django.conf import settings
5 register = template.Library()
8 class MembersofNode(template.Node):
9 def __init__(self, collection, model, as_var):
10 self.collection = template.Variable(collection)
11 self.model = template.Variable(model)
14 def render(self, context):
16 collection = self.collection.resolve(context)
17 model = self.model.resolve(context)
18 context[self.as_var] = collection.members.with_model(model)
21 return settings.TEMPLATE_STRING_IF_INVALID
24 def do_membersof(parser, token):
26 {% membersof <collection> with <model> as <var> %}
28 params=token.split_contents()
32 raise template.TemplateSyntaxError('"%s" template tag requires six parameters' % tag)
34 if params[2] != 'with':
35 raise template.TemplateSyntaxError('"%s" template tag requires the third parameter to be "with"' % tag)
38 raise template.TemplateSyntaxError('"%s" template tag requires the fifth parameter to be "as"' % tag)
40 return MembersofNode(collection=params[1], model=params[3], as_var=params[5])
43 register.tag('membersof', do_membersof)