mirror of
https://github.com/jazzband/django-categories.git
synced 2026-03-16 22:30:24 +00:00
Added a new templatetag to retrieve the top level categories
This commit is contained in:
parent
176edd7736
commit
e69cfe9627
1 changed files with 30 additions and 1 deletions
|
|
@ -102,4 +102,33 @@ def display_path_as_ul(category):
|
|||
cat = get_category(category)
|
||||
|
||||
return {}
|
||||
|
||||
|
||||
|
||||
class TopLevelCategoriesNode(template.Node):
|
||||
def __init__(self, varname):
|
||||
self.varname = varname
|
||||
|
||||
def render(self, context):
|
||||
context[self.varname] = Category.objects.filter(parent=None).order_by('name')
|
||||
return ''
|
||||
|
||||
|
||||
def get_top_level_categories(parser, token):
|
||||
"""
|
||||
Retrives an alphabetical list of all the categories with with no parents.
|
||||
|
||||
Syntax::
|
||||
|
||||
{% get_top_level_categories as categories %}
|
||||
|
||||
Returns an list of categories
|
||||
|
||||
"""
|
||||
bits = token.contents.split()
|
||||
if len(bits) != 3:
|
||||
raise template.TemplateSyntaxError, "Tag %s must have 2 arguments." % bits[0]
|
||||
if bits[1] != 'as':
|
||||
raise template.TemplateSyntaxError, "First argyment must be 'as'."
|
||||
return TopLevelCategoriesNode(bits[2])
|
||||
|
||||
register.tag(get_top_level_categories)
|
||||
Loading…
Reference in a new issue