Login

Navigation list from YAML file

Author:
sean2000
Posted:
January 31, 2008
Language:
Python
Version:
.96
Tags:
tag navigation yaml
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 to handle navigation item selection by SmileyChris 5 years, 11 months ago
  2. Non-Javascript select list navigator by shacker 6 years, 5 months ago
  3. Menu/navigation bars in a tag by ep 8 years, 1 month ago
  4. Generate a CSV file for a model by mssnlayam 8 years, 5 months ago
  5. Python Google Translate AJAX module by vsergeyev 5 years, 6 months ago

Comments

Please login first before commenting.