Commits

Matthew Schinckel  committed 7a127f2

Allow passing extra arguments to url: in menu items.

  • Participants
  • Parent commits 5572289

Comments (0)

Files changed (1)

File menus/templatetags/menu_item.py

 def resolve_path(path, context):
     current = context
     for obj in path.split('.'):
-        current = current[obj]
+        try:
+            current = current[obj]
+        except TypeError:
+            current = getattr(current, obj)
     
     return current
     
     """
     The template node for generating a menu item.
     """
-    def __init__(self, template_file, url, text, classes=None):
+    def __init__(self, template_file, url, text, classes=None, *url_args):
         """
         template_file : the name of the template that should be used for each
                         menu item. defaults to 'menu/item.html', but you can
         super(MenuItem, self).__init__()
         self.template_file = template_file or 'menu/item.html'
         self.url = url
+        self.url_args = url_args
         self.text = text
         self.classes = classes if classes else ""
         self.icons = []
             url = self.url
             
         if url.startswith('url:'):
-            url = reverse(url[4:])
+            if self.url_args:
+                url_args = [resolve_path(x, context) for x in self.url_args]
+                url = reverse(url[4:], args=url_args)
+            else:
+                url = reverse(url[4:])
         elif url.startswith('http'):
             pass
         else:
     The actual template tag.
     """
     def inner(_parser, token):
-        error_message = "'menu_item' tag requires at least 2, at most 3 arguments"
+        error_message = "'menu_item' tag requires at least 2, arguments"
     
         try:
             parts = token.split_contents()
         except ValueError:
             raise template.TemplateSyntaxError(error_message)
-    
-        if not (3 <= len(parts) <= 4):
+        
+        if not (3 <= len(parts)):
             raise template.TemplateSyntaxError(error_message)
-    
+        
         # parts[0] is the name of the tag.
         return MenuItem(template, *parts[1:])
     return inner