[PATCH 1 of 2] Add "nonempty" template filter

Rocco Rutte pdmef at gmx.net
Sun Apr 5 07:50:21 CDT 2009


Hi,

* Martin Geisler wrote:

> if str implies str != '' since the empty string is False in Python.

Alright, here we go.

Rocco

# HG changeset patch
# User Rocco Rutte <pdmef at gmx.net>
# Date 1238935497 -7200
# Node ID 902ce3034331de189691a4194d56f5a40056b298
# Parent  58ba4b86ae136721f1d13f60016501b254e4d5f2
Add "nonempty" template filter

It ensures at least "(none)" is returned in case no argument is passed
in. This is primarily useful to render empty changelog messages for
hgweb but may be useful for others, too.

diff --git a/mercurial/templatefilters.py b/mercurial/templatefilters.py
--- a/mercurial/templatefilters.py
+++ b/mercurial/templatefilters.py
@@ -154,6 +154,9 @@ def json(obj):
     else:
         raise TypeError('cannot encode type %s' % obj.__class__.__name__)
 
+def nonempty(str):
+    return str or "(none)"
+
 filters = {
     "addbreaks": nl2br,
     "basename": os.path.basename,
@@ -183,4 +186,5 @@ filters = {
     "stringescape": lambda x: x.encode('string_escape'),
     "xmlescape": xmlescape,
     "json": json,
+    "nonempty": nonempty,
 }


More information about the Mercurial-devel mailing list