Tag to generate a navigation list from a YAML file.
Generates a series of divs but to be more semantically correct could do a ul or dl.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | from django import template
from django.conf import settings
from django.template import Library, Node, TemplateSyntaxError
from settings import PROJECT_ROOT
import yaml
from os.path import join
register = Library()
@register.simple_tag
def navlist():
'''Generates a navigation list from a yaml file with syntax:
Heading1:
- name: A link
href: /alink
- name: Another link
href: /anotherlink
Heading2:
- name: A link
href: /alink
- name: Another link
href: /anotherlink
'''
out = []
tree = yaml.load(open(join(PROJECT_ROOT, 'menu.yaml')).read())
for section in tree.keys():
out.append('''\
<div class="heading"
id="heading-%s">%s</div>
<div class="section" id="section-%s">
''' % (section.lower(), section.lower(), section, section.lower()))
items = tree[section]
for item in items:
out.append('''\
<div>
<a class="navlink" href="%s">%s</a>
</div>\
''' % (item.get('href'), item.get('name')))
out.append('</div>')
return ''.join(out)
|
More like this
- Template tag - list punctuation for a list of items by shapiromatron 10 months, 1 week ago
- JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 10 months, 2 weeks ago
- Serializer factory with Django Rest Framework by julio 1 year, 5 months ago
- Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 6 months ago
- Help text hyperlinks by sa2812 1 year, 6 months ago
Comments
Please login first before commenting.