Renamed navigation to shipherd.
[philo.git] / contrib / shipherd / templatetags / shipherd.py
diff --git a/contrib/shipherd/templatetags/shipherd.py b/contrib/shipherd/templatetags/shipherd.py
new file mode 100644 (file)
index 0000000..fec700b
--- /dev/null
@@ -0,0 +1,72 @@
+from django import template
+from django.conf import settings
+from django.utils.safestring import mark_safe
+from philo.contrib.shipherd.models import Navigation
+from mptt.templatetags.mptt_tags import RecurseTreeNode, cache_tree_children
+
+
+register = template.Library()
+
+
+class RecurseNavigationNode(RecurseTreeNode):
+       def __init__(self, template_nodes, instance_var):
+               self.template_nodes = template_nodes
+               self.instance_var = instance_var
+       
+       def _render_node(self, context, node, request):
+               bits = []
+               context.push()
+               for child in node.get_children():
+                       context['node'] = child
+                       bits.append(self._render_node(context, child, request))
+               context['node'] = node
+               context['children'] = mark_safe(u''.join(bits))
+               context['active'] = node.is_active(request)
+               rendered = self.template_nodes.render(context)
+               context.pop()
+               return rendered
+       
+       def render(self, context):
+               try:
+                       request = context['request']
+               except KeyError:
+                       return ''
+               
+               instance = self.instance_var.resolve(context)
+               roots = cache_tree_children(Navigation.objects.closest_navigation(instance))
+               bits = [self._render_node(context, node, request) for node in roots]
+               return ''.join(bits)
+
+
+@register.tag
+def recursenavigation(parser, token):
+       """
+       Based on django-mptt's recursetree templatetag. In addition to {{ node }} and {{ children }},
+       sets {{ active }} in the context.
+       
+       Note that the tag takes one variable, navigation, which is a Navigation instance.
+       
+       Usage:
+               <ul>
+                       {% recursenavigation navigation %}
+                               <li{% if active %} class='active'{% endif %}>
+                                       {{ node.name }}
+                                       {% if not node.is_leaf_node %}
+                                               <ul>
+                                                       {{ children }}
+                                               </ul>
+                                       {% endif %}
+                               </li>
+                       {% endrecursenavigation %}
+               </ul>
+       """
+       bits = token.contents.split()
+       if len(bits) != 2:
+               raise template.TemplateSyntaxError(_('%s tag requires an instance') % bits[0])
+       
+       instance_var = template.Variable(bits[1])
+       
+       template_nodes = parser.parse(('endrecursenavigation',))
+       parser.delete_first_token()
+       
+       return RecurseNavigationNode(template_nodes, instance_var)
\ No newline at end of file