Mike Orr avatar Mike Orr committed 5a7e91d

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

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)
     
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.