2 The collection template tags are automatically included as builtins if :mod:`philo` is an installed app.
6 from django import template
7 from django.conf import settings
8 from django.contrib.contenttypes.models import ContentType
11 register = template.Library()
14 class MembersofNode(template.Node):
15 def __init__(self, collection, model, as_var):
16 self.collection = template.Variable(collection)
20 def render(self, context):
22 collection = self.collection.resolve(context)
23 context[self.as_var] = collection.members.with_model(self.model)
30 def membersof(parser, token):
32 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.
36 {% membersof <collection> with <app_label>.<model_name> as <var> %}
39 params=token.split_contents()
43 raise template.TemplateSyntaxError('"%s" template tag requires six parameters' % tag)
45 if params[2] != 'with':
46 raise template.TemplateSyntaxError('"%s" template tag requires the third parameter to be "with"' % tag)
49 app_label, model = params[3].strip('"').split('.')
50 ct = ContentType.objects.get(app_label=app_label, model=model)
52 raise template.TemplateSyntaxError('"%s" template tag option "with" requires an argument of the form app_label.model (see django.contrib.contenttypes)' % tag)
53 except ContentType.DoesNotExist:
54 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)
57 raise template.TemplateSyntaxError('"%s" template tag requires the fifth parameter to be "as"' % tag)
59 return MembersofNode(collection=params[1], model=ct.model_class(), as_var=params[5])