Commits

Anonymous committed 4479dc4

better peppercorn tag rendering and handle IDs properly

Comments (0)

Files changed (2)

pyramid_simpleform/renderers.py

         """
         return HTML.tag("div", self.csrf(name), style="display:none;")
 
-    def __start__(self, type, name=None):
+    def start_hidden_tag(self, type, name=None, id=''):
         """
         Renders peppercorn __start__ hidden tags with given values.
 
         For example:
 
-        form.__start__('mapping', 'series')
+        renderer.start_hidden_tag('mapping', 'series')
         >>> <input type="hidden" name="__start__" value="series:mapping" />
 
         :versionadded: 0.7
         value = type
         if name:
             value = name + ":" + value
-        return self.hidden('__start__', value)
 
+        return HTML.tag(
+            "div", 
+            self.hidden('__start__', value, id=id), 
+            style="display:none;"
+        )
 
-    def __end__(self, value=None):
+    def end_hidden_tag(self, value=None, id=''):
         """
         Renders peppercorn __end__ hidden tag
 
         For example:
 
-        form.__end__()
+        renderer.end_hidden_tag()
         >>> <input type="hidden" name="__end__" />
 
         :versionadded: 0.7
         """
+        return HTML.tag(
+            "div",
+            self.hidden('__end__', value, id=id),
+            style="display:none;"
+        )
 
-        return self.hidden('__end__', value)
+    def end_hidden_tags(self, num_tags):
+        """
+        Prints a number of peppercorn hidden __end__ tags
+
+        :versionadded: 0.7
+        """
+
+        rv = []
+
+        for i in xrange(num_tags):
+            rv.append(self.hidden('__end__', id=''))
+
+        return HTML.tag(
+            "div",
+            tags.literal("".join(rv)),
+            style="display:none;"
+        )
 
     def hidden_tag(self, *names):
         """
         """
         Outputs text input.
         """
-        id = id or name
-        return tags.text(name, self.value(name, value), id, **attrs)
+        return tags.text(
+            name, 
+            self.value(name, value), 
+            self._get_id(id, name), 
+            **attrs
+        )
 
     def file(self, name, value=None, id=None, **attrs):
         """
         Outputs file input.
         """
-        id = id or name
-        return tags.file(name, self.value(name, value), id, **attrs)
+        return tags.file(
+            name, 
+            self.value(name, value), 
+            self._get_id(id, name), 
+            **attrs
+        )
 
     def hidden(self, name, value=None, id=None, **attrs):
         """
         Outputs hidden input.
         """
-        id = id or name
-        return tags.hidden(name, self.value(name, value), id, **attrs)
+        return tags.hidden(
+            name, 
+            self.value(name, value), 
+            self._get_id(id, name), 
+            **attrs
+        )
 
     def radio(self, name, value=None, checked=False, label=None, **attrs):
         """
         """
         Outputs submit button.
         """
-        id = id or name
-        return tags.submit(name, self.value(name, value), id, **attrs)
+        return tags.submit(
+            name, 
+            self.value(name, value), 
+            self._get_id(id, name), 
+            **attrs
+        )
 
     def select(self, name, options, selected_value=None, id=None, **attrs):
         """
         Outputs <select> element.
         """
-        id = id or name
-        return tags.select(name, self.value(name, selected_value), 
-                           options, id, **attrs)
+        return tags.select(
+            name, 
+            self.value(name, selected_value), 
+            options, 
+            self._get_id(id, name), 
+            **attrs
+        )
 
     def checkbox(self, name, value="1", checked=False, label=None, id=None, 
                  **attrs):
         Outputs checkbox input.
         """
     
-        id = id or name
-        return tags.checkbox(name, value, self.value(name), label, id, **attrs)
+        return tags.checkbox(
+            name, 
+            value, 
+            self.value(name), 
+            label, 
+            self._get_id(id, name), 
+            **attrs
+        )
 
     def textarea(self, name, content="", id=None, **attrs):
         """
         Outputs <textarea> element.
         """
-        id = id or name
-        return tags.textarea(name, self.value(name, content), id, **attrs)
+
+        return tags.textarea(
+            name, 
+            self.value(name, content), 
+            self._get_id(id, name), 
+            **attrs
+        )
 
     def password(self, name, value=None, id=None, **attrs):
         """
         Outputs a password input.
         """
-        id = id or name
-        return tags.password(name, self.value(name, value), id, **attrs)
+        return tags.password(
+            name, self.value(name, value), 
+            self._get_id(id, name), 
+            **attrs)
 
     def is_error(self, name):
         """
         label = label or name.capitalize()
         return HTML.tag("label", label, **attrs)
 
+    def _get_id(self, id, name):
+        if id is None:
+            id = name
+        return id
+

pyramid_simpleform/tests.py

                 '<div style="display:none;"><input id="_csrf" name="_csrf" '
                 'type="hidden" value="csrft" /></div>')
 
-    def test_start(self):
+    def test_start_hidden_tag(self):
 
         from pyramid_simpleform.form import Form
         from pyramid_simpleform.renderers import FormRenderer
         form = Form(request, SimpleColanderSchema())
         renderer = FormRenderer(form)
 
-        self.assert_(renderer.__start__('mapping', 'series') == \
-                '<input id="__start__" name="__start__" type="hidden" value="series:mapping" />')
+        self.assert_(renderer.start_hidden_tag('mapping', 'series') == \
+                '<div style="display:none;"><input name="__start__" type="hidden" value="series:mapping" /></div>')
 
-    def test_end(self):
+    def test_end_hidden_tag(self):
 
         from pyramid_simpleform.form import Form
         from pyramid_simpleform.renderers import FormRenderer
         form = Form(request, SimpleColanderSchema())
         renderer = FormRenderer(form)
 
-        self.assert_(renderer.__end__() == \
-                '<input id="__end__" name="__end__" type="hidden" />')
+        self.assert_(renderer.end_hidden_tag() == \
+                '<div style="display:none;"><input name="__end__" type="hidden" /></div>')
+ 
+    def test_end_hidden_tags(self):
+
+        from pyramid_simpleform.form import Form
+        from pyramid_simpleform.renderers import FormRenderer
+
+        request = testing.DummyRequest()
+        form = Form(request, SimpleColanderSchema())
+        renderer = FormRenderer(form)
+
+        self.assert_(renderer.end_hidden_tags(1) == \
+                '<div style="display:none;"><input name="__end__" type="hidden" /></div>')
  
  
     def test_text(self):