1. Curtis Maloney
  2. django-sniplates

Commits

funkybob  committed 5ca9dbb

Removed 'row' from form tag.
Pass all extra options to context for form, use, and field -- act like with tag

  • Participants
  • Parent commits dd23f95
  • Branches default

Comments (0)

Files changed (2)

File setup.py

View file
  • Ignore whitespace
 from setuptools import setup, find_packages
 
 setup( name='django-sniplates',
-    version = '1.1',
+    version = '1.1.1',
     description = 'Django Template Snippet libraries',
     author = 'Curtis Maloney',
     author_email = 'curtis@tinbrain.net',

File sniplates/templatetags/form.py

View file
  • Ignore whitespace
         # Grab the template snippets
         self.templates = resolve_blocks(lib, context)
 
-        # Do we have a default row template?
-        row = self.options.get('row', 'row')
-        if row:
-            row = self.templates.get_block(row)
-
         # Find the parent snippet
-        template_name = options.get('form', 'form')
+        template_name = options.pop('form', 'form')
         template = self.templates.get_block(template_name)
 
         # Add ourself to the context
-        context.update({
+        options.update({
             'form_tag': self,
-            'row_tmpl': row,
             'form': self.form,
             'used_fields': set(),
         })
 
+        context.update(options)
         output = template.render(context)
         context.pop()
 
     bits.pop(0)
     widget = parser.compile_filter(bits.pop(0))
 
-    return UseNode(widget)
+    options = {}
+    for bit in bits:
+        if '=' in bit:
+            key, val = bit.split('=', 1)
+            options[key] = parser.compile_filter(val)
+        else:
+            raise template.TemplateSyntaxError(
+                "Unexpected positional argument option: %r" % bit
+            )
+
+    return UseNode(widget, options)
 
 class UseNode(template.Node):
     def __init__(self, widget):
         self.widget = widget
     def render(self, context):
         widget = self.widget.resolve(context)
-        return context['form_tag'].templates.get_block(widget).render(context)
+        options = resolve_dict(self.options, context)
+        context.update(options)
+        output = context['form_tag'].templates.get_block(widget).render(context)
+        context.pop()
+        return output
 
 @register.tag
 def field(parser, token):
-    '''{% field name [widget="..."] [library="..."] %}'''
+    '''{% field name [widget="..."] %}'''
     bits = token.split_contents()
     bits.pop(0)
     field = parser.compile_filter(bits.pop(0))
 
         context['used_fields'].add(field.name)
 
-        if 'widget' in options:
-            tmpl = form_tag.templates.get_block(options['widget'])
+        widget = options.pop('widget', None)
+        if widget:
+            tmpl = form_tag.templates.get_block(widget)
         else:
             tmpl = form_tag.get_template_for_field(field)
 
         # Update the context
-        context.update({'field': field})
+        options['field'] = field
+        context.update(options)
 
         # Render
         output = tmpl.render(context)