Anonymous committed 4034e89

[svn] add image support to button_to
Patch by Alex Conrad
Fixes #184

Comments (0)

Files changed (3)

 WebHelpers ChangeLog
 0.3.1 (**svn**)
+* Added image support to button_to. Patch by Alex Conrad. Fixes #184.
 * Fix radio_button and submit_to_remote not handling unicode values.
   Fixes #235.
 * Added support for the defer attribute to javascript_include_tag. Suggested by


         self.assertEqual("<form method=\"GET\" action=\"\" class=\"button-to\"><div><input type=\"submit\" value=\"Hello\" /></div></form>",
             button_to("Hello", "", method='GET'))
+    def test_button_to_with_img(self):
+        self.assertEqual('<form method="POST" action="/content/edit/3" class="button-to"><div><input alt="Edit" src="/images/icon_delete.gif" type="image" value="Edit" /></div></form>',
+                         button_to("Edit", url(action='edit', id=3), type='image', src='icon_delete.gif'))
+        self.assertEqual('<form method="POST" action="/content/submit/3" class="button-to"><div><input alt="Complete the form" src="/images/submit.png" type="image" value="Submit" /></div></form>',
+                         button_to("Submit", url(action='submit', id=3), type='image', src='submit', alt='Complete the form'))
     def test_link_tag_with_straight_url(self):
         self.assertEqual("<a href=\"\">Hello</a>", link_to("Hello", ""))


 from routes import url_for, request_config
 import tags
+from asset_tag import compute_public_path
 from javascript import *
 from webhelpers.util import html_escape
     'button-to', to which you can attach CSS styles for display
+    The submit button itself will be displayed as an image if you provide both
+    ``type`` and ``src`` as followed:
+         type='image', src='icon_delete.gif'
+    The ``src`` path will be computed as the image_tag() computes it's ``source``
+    argument.
     Example 1::
         # inside of controller for "feeds"
             <input onclick="return confirm('Are you sure?');" value="Destroy" type="submit" />
+    Example 3::
+        # Button as an image.
+        >>> button_to("Edit", url(action='edit', id=3), type='image', src='icon_delete.gif')
+        <form method="POST" action="/feeds/edit/3" class="button-to">
+        <div><input alt="Edit" src="/images/icon_delete.gif" type="image" value="Edit" /></div>
+        </form>
     *NOTE*: This method generates HTML code that represents a form.
     Forms are "block" content, which means that you should not try to
         url, name = url, name or url
-    html_options.update(dict(type='submit', value=name))
+    submit_type = html_options.get('type')
+    img_source = html_options.get('src')
+    if submit_type == 'image' and img_source:
+        html_options.update(dict(type=submit_type, value=name,
+                                 alt=html_options.get('alt', name)))
+        html_options['src'] = compute_public_path(img_source, 'images', 'png')
+    else:
+        html_options.update(dict(type='submit', value=name))
     return """<form method="%s" action="%s" class="button-to"><div>""" % \
         (form_method, tags.escape_once(url)) + method_tag + \