Commits

Matthew Schinckel committed edcde98

Refactor template tag to allow for li-menu-item template tag.

Comments (0)

Files changed (2)

menus/templates/menu/li-item.html

+<li class="{{ classes }}">{% include "menu/item.html" %}</li>

menus/templatetags/menu_item.py

         if getattr(x, 'func_code', None) and x.func_code.co_varnames[0] in ["user", "u"]
     ]
 
+def resolve_path(path, context):
+    current = context
+    for obj in path.split('.'):
+        current = current[obj]
+    
+    return current
+    
 class MenuItem(template.Node):
     """
     The template node for generating a menu item.
         """
         super(MenuItem, self).__init__()
         self.template_file = template_file or 'menu/item.html'
-        url = url.strip('\'"')
-        if url.startswith('url:'):
-            self.url = reverse(url[4:], args=[])
-        else:
-            self.url = url
-            
-        self.text = text.strip('\'"')
+        self.url = url
+        self.text = text
         self.classes = classes or ""
         self.nodelist = []
         
         Basically, we need `request` to be in the context, so we can access
         the logged in user.
         """
-        if self not in context.render_context:
-            context.render_context[self] = {
-                'url': self.url,
-                'text': self.text,
-                'classes': self.classes
-            }
+        if self.url[0] in "\"'":
+            url = self.url.strip("'\"")
+            if url.startswith('url:'):
+                url = reverse(url[4:])
+        else:
+            url = resolve_path(self.url, context)
+        
+        if self.text[0] in "\"'":
+            text = self.text.strip("\"'")
+        else:
+            text = resolve_path(self.text, context)
+
+        if not self.classes or self.classes[0] in "\"'":
+            classes = self.classes.strip("\"'")
+        else:
+            classes = resolve_path(self.classes, context)
+        
+        context.render_context[self] = {
+            'url': url,
+            'text': text,
+            'classes': classes
+        }
         
         local = dict(context.render_context[self])
         
         return self.nodelist.render(new_context)
         
     
-def tested_menu_item(_parser, token):
+def base_menu_item(template="menu/item.html"):
     """
     The actual template tag.
     """
-    error_message = "'menu_item' tag requires at least 2, at most 3 arguments"
+    def inner(_parser, token):
+        error_message = "'menu_item' tag requires at least 2, at most 3 arguments"
     
-    try:
-        parts = token.split_contents()
-    except ValueError:
-        raise template.TemplateSyntaxError(error_message)
+        try:
+            parts = token.split_contents()
+        except ValueError:
+            raise template.TemplateSyntaxError(error_message)
     
-    if not (3 <= len(parts) <= 4):
-        raise template.TemplateSyntaxError(error_message)
+        if not (3 <= len(parts) <= 4):
+            raise template.TemplateSyntaxError(error_message)
     
-    # parts[0] is the name of the tag.
-    return MenuItem('menu/item.html', *parts[1:])
+        # parts[0] is the name of the tag.
+        return MenuItem(template, *parts[1:])
+    return inner
 
-register.tag('menu_item', tested_menu_item)
+register.tag('menu_item', base_menu_item())
+register.tag('li_menu_item', base_menu_item('menu/li-item.html'))
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.