counter templatetag

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
from django import template

register = template.Library()

@register.tag(name='counter')
def do_counter(parser, token):
    try:
        tag_name, args = token.contents.split(None, 1)
    except ValueError:
        raise template.TemplateSyntaxError("'counter' node requires a variable name.")
    return CounterNode(args)

class CounterNode(template.Node):
    def __init__(self, varname):
        self.varname = varname

    def render(self, context):
        try:
            var = template.resolve_variable(self.varname, context)
        except:
            var = 0
        deep = len(context.dicts)-1
        context.dicts[deep][self.varname] = var+1
        return ''

More like this

  1. Digg-like pagination by SmileyChris 3 years, 11 months ago
  2. Switch template tag by adurdin 4 years, 9 months ago
  3. Template tag to create a list from one or more variables and/or literals by davidchambers 2 years, 8 months ago
  4. Page numbers with ... like in Digg by Ciantic 4 years, 1 month ago
  5. Paginator TemplateTag by trbs 5 years, 1 month ago

Comments

(Forgotten your password?)