- for i, bit in enumerate(split):
- if bit == 'as':
- as_index = i
- break
- if as_index is not None:
- try:
- context_var = split[as_index + 1]
- except IndexError:
- raise template.TemplateSyntaxError("Context variable assignment " +
- "must take the form of {%% %r object.example_set.all ... as " +
- "context_var_name %%}" % split[0])
- del split[as_index:as_index + 2]
- if len(split) == 2:
- return AutoPaginateNode(split[1])
- elif len(split) == 3:
- return AutoPaginateNode(split[1], paginate_by=split[2],
- context_var=context_var)
- elif len(split) == 4:
- try:
- orphans = int(split[3])
- except ValueError:
- raise template.TemplateSyntaxError(u'Got %s, but expected integer.'
- % split[3])
- return AutoPaginateNode(split[1], paginate_by=split[2], orphans=orphans,
- context_var=context_var)
- else:
- raise template.TemplateSyntaxError('%r tag takes one required ' +
- 'argument and one optional argument' % split[0])
+ orphans = None
+ word = None
+ try:
+ word = i.next()
+ assert word == "autopaginate"
+ queryset_var = i.next()
+ word = i.next()
+ if word != "as":
+ paginate_by = word
+ try:
+ paginate_by = int(paginate_by)
+ except ValueError:
+ pass
+ word = i.next()
+ if word != "as":
+ orphans = word
+ try:
+ orphans = int(orphans)
+ except ValueError:
+ pass
+ word = i.next()
+ assert word == "as"
+ context_var = i.next()
+ except StopIteration:
+ pass
+ if queryset_var is None:
+ raise template.TemplateSyntaxError(
+ "Invalid syntax. Proper usage of this tag is: "
+ "{%% autopaginate QUERYSET [PAGINATE_BY] [ORPHANS]"
+ " [as CONTEXT_VAR_NAME] %%}"
+ )
+ return AutoPaginateNode(queryset_var, paginate_by, orphans, context_var)