Commits

Mike Orr committed 4053a49

Add 'default' argument to ul() and ol().

Comments (0)

Files changed (1)

webhelpers/html/tags.py

            # Table tags
            "th_sortable",
            # Other non-form tags
+           "ol",
+           "ul",
            "image",
            # Head tags
            "auto_discovery_link",
 
 #### Other non-form tags
 
-def ul(items, li_attrs=None, **attrs):
+def ul(items, default=literal(""), li_attrs=None, **attrs):
     R"""Return an unordered list with each item wrapped in <li>.
 
     ``items``
         list of strings.
 
+    ``default``
+        value returned if there are no items in the list.
+
     ``li_attrs``
         dict of attributes for the <li> tags.
 
     literal(u'<ul>\n<li>foo</li>\n<li>bar</li>\n</ul>')
     >>> ul(["A", "B"], li_attrs={"class_": "myli"}, class_="mylist") 
     literal(u'<ul class="mylist">\n<li class="myli">A</li>\n<li class="myli">B</li>\n</ul>')
+    >>> ul([])
+    literal(u'')
     """
     li_attrs = li_attrs or {}
-    return _list("ul", items, attrs, li_attrs)
+    return _list("ul", items, default, attrs, li_attrs)
 
-def ol(items, li_attrs=None, **attrs):
+def ol(items, default=literal(""), li_attrs=None, **attrs):
     R"""Return an ordered list with each item wrapped in <li>.
 
     ``items``
         list of strings.
 
+    ``default``
+        value returned if there are no items in the list.
+
     ``li_attrs``
         dict of attributes for the <li> tags.
 
     literal(u'<ol>\n<li>foo</li>\n<li>bar</li>\n</ol>')
     >>> ol(["A", "B"], li_attrs={"class_": "myli"}, class_="mylist") 
     literal(u'<ol class="mylist">\n<li class="myli">A</li>\n<li class="myli">B</li>\n</ol>')
+    >>> ol([])
+    literal(u'')
     """
     li_attrs = li_attrs or {}
-    return _list("ol", items, attrs, li_attrs)
+    return _list("ol", items, default, attrs, li_attrs)
 
-def _list(tag, items, attrs, li_attrs):
+def _list(tag, items, default, attrs, li_attrs):
     content = [HTML.li(x, **li_attrs) for x in items]
+    if not content:
+        return default
     content = [""] + content + [""]
     content = literal("\n").join(content)
     return getattr(HTML, tag)(content, **attrs)
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.