2 The collection template tags are automatically included as builtins if :mod:`philo` is an installed app.
4 .. templatetag:: membersof
9 Given a collection and a content type, sets the results of :meth:`collection.members.with_model <.CollectionMemberManager.with_model>` as a variable in the context.
13 {% membersof <collection> with <app_label>.<model_name> as <var> %}
17 from django import template
18 from django.conf import settings
19 from django.contrib.contenttypes.models import ContentType
22 register = template.Library()
25 class MembersofNode(template.Node):
26 def __init__(self, collection, model, as_var):
27 self.collection = template.Variable(collection)
31 def render(self, context):
33 collection = self.collection.resolve(context)
34 context[self.as_var] = collection.members.with_model(self.model)
40 def do_membersof(parser, token):
42 {% membersof <collection> with <app_label>.<model_name> as <var> %}
45 params=token.split_contents()
49 raise template.TemplateSyntaxError('"%s" template tag requires six parameters' % tag)
51 if params[2] != 'with':
52 raise template.TemplateSyntaxError('"%s" template tag requires the third parameter to be "with"' % tag)
55 app_label, model = params[3].strip('"').split('.')
56 ct = ContentType.objects.get(app_label=app_label, model=model)
58 raise template.TemplateSyntaxError('"%s" template tag option "with" requires an argument of the form app_label.model (see django.contrib.contenttypes)' % tag)
59 except ContentType.DoesNotExist:
60 raise template.TemplateSyntaxError('"%s" template tag option "with" requires an argument of the form app_label.model which refers to an installed content type (see django.contrib.contenttypes)' % tag)
63 raise template.TemplateSyntaxError('"%s" template tag requires the fifth parameter to be "as"' % tag)
65 return MembersofNode(collection=params[1], model=ct.model_class(), as_var=params[5])
68 register.tag('membersof', do_membersof)