Fixing a bug in BlogView's tag_view where it would provide a QuerySet as `tag` instea...
[philo.git] / templatetags / collections.py
1 from django import template
2 from django.conf import settings
3
4
5 register = template.Library()
6
7
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)
12                 self.as_var = as_var
13                 
14         def render(self, context):
15                 try:
16                         collection = self.collection.resolve(context)
17                         model = self.model.resolve(context)
18                         context[self.as_var] = collection.members.with_model(model)
19                 except:
20                         pass
21                 return settings.TEMPLATE_STRING_IF_INVALID
22
23
24 def do_membersof(parser, token):
25         """
26         {% membersof <collection> with <model> as <var> %}
27         """
28         params=token.split_contents()
29         tag = params[0]
30         
31         if len(params) < 6:
32                 raise template.TemplateSyntaxError('"%s" template tag requires six parameters' % tag)
33                 
34         if params[2] != 'with':
35                 raise template.TemplateSyntaxError('"%s" template tag requires the third parameter to be "with"' % tag)
36                 
37         if params[4] != 'as':
38                 raise template.TemplateSyntaxError('"%s" template tag requires the fifth parameter to be "as"' % tag)
39         
40         return MembersofNode(collection=params[1], model=params[3], as_var=params[5])
41
42
43 register.tag('membersof', do_membersof)