+def register_counter(f):
+ """Turns a simple counting function into a registered counter tag.
+
+ You can run a counter tag as a simple {% tag_name %} tag, or
+ as {% tag_name var_name %} to store the result in a variable.
+
+ """
+ @wraps(f)
+ def wrapped(parser, token):
+ try:
+ tag_name, args = token.contents.split(None, 1)
+ except ValueError:
+ args = None
+ return StatsNode(f(), args)
+
+ return register.tag(wrapped)
+
+
+@register_counter
+def count_books_all():
+ return Book.objects.all().count()