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)
Comments