Login

Navigation list from YAML file

Author:
sean2000
Posted:
January 31, 2008
Language:
Python
Version:
.96
Score:
1 (after 1 ratings)

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

  1. Template tag - list punctuation for a list of items by shapiromatron 2 months ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 2 months, 1 week ago
  3. Serializer factory with Django Rest Framework by julio 9 months, 1 week ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 9 months, 4 weeks ago
  5. Help text hyperlinks by sa2812 10 months, 3 weeks ago

Comments

Please login first before commenting.