Commits

Mike Orr committed 5a7e91d

Make ul() and ol() return an empty list if no items and no default is
specified.

  • Participants
  • Parent commits bccfea2

Comments (0)

Files changed (1)

webhelpers/html/tags.py

 
 #### Other non-form tags
 
-def ul(items, default=literal(""), li_attrs=None, **attrs):
+def ul(items, default=None, 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.
+        value returned _instead of the <ul>_ if there are no items in the list.
+        If ``None``, return an empty <ul>.
 
     ``li_attrs``
         dict of attributes for the <li> tags.
     literal(u'<ul class="mylist">\n<li class="myli">A</li>\n<li class="myli">B</li>\n</ul>')
     >>> ul([])
     literal(u'')
+    >>> ul([], default=literal("<span class='no-data'>No data</span>"))
+    literal(u'<span class='no-data'>No data</span>')
+    >>> ul(["A"], default="NOTHING")
+    literal(u'<ul>\n<li class="myli">A</li>\n</ul>')
     """
     li_attrs = li_attrs or {}
     return _list("ul", items, default, attrs, li_attrs)
         list of strings.
 
     ``default``
-        value returned if there are no items in the list.
+        value returned _instead of the <ol>_ if there are no items in the list.
+        If ``None``, return an empty <ol>.
 
     ``li_attrs``
         dict of attributes for the <li> tags.
 
 def _list(tag, items, default, attrs, li_attrs):
     content = [HTML.li(x, **li_attrs) for x in items]
-    if not content:
+    if content:
+        content = [""] + content + [""]
+    elif default is not None:
         return default
-    content = [""] + content + [""]
     content = literal("\n").join(content)
     return getattr(HTML, tag)(content, **attrs)