]> git.ipfire.org Git - thirdparty/jinja.git/commitdiff
map filter can use False as default 1356/head
authorBALaka-18 <balaka2605@gmail.com>
Mon, 22 Feb 2021 15:56:22 +0000 (21:26 +0530)
committerDavid Lord <davidism@gmail.com>
Thu, 25 Feb 2021 01:16:01 +0000 (17:16 -0800)
CHANGES.rst
src/jinja2/filters.py
tests/test_filters.py

index f1e0ee0df06f56ec48fdd16dd661c9f037699a5d..c53d67e1df292db22ea8b55543dc5486c102d470 100644 (file)
@@ -30,6 +30,8 @@ Unreleased
     has been updated to be more efficient and match more cases. URLs
     without a scheme are linked as ``https://`` instead of ``http://``.
     :issue:`522, 827, 1172`, :pr:`1195`
+-   Filters that get attributes, such as ``map`` and ``groupby``, can
+    use a false or empty value as a default. :issue:`1331`
 
 
 Version 2.11.3
index 9fb52ed4c3fa489a275ad6f42f89b6ff92d622bc..7c95dcebb9da2ccd19ca6d3051ae4b09a52170ff 100644 (file)
@@ -67,7 +67,7 @@ def make_attrgetter(environment, attribute, postprocess=None, default=None):
         for part in attribute:
             item = environment.getitem(item, part)
 
-            if default and isinstance(item, Undefined):
+            if default is not None and isinstance(item, Undefined):
                 item = default
 
         if postprocess is not None:
index 3cd0fdd11f7810c1dbae5020583ad6d2e6748050..efc82bcf81f9cab3e5ecc58f51ed2d05b1e2405a 100644 (file)
@@ -666,6 +666,12 @@ class TestFilter:
         tmpl = env.from_string(
             '{{ users|map(attribute="lastname", default="smith")|join(", ") }}'
         )
+        test_list = env.from_string(
+            '{{ users|map(attribute="lastname", default=["smith","x"])|join(", ") }}'
+        )
+        test_str = env.from_string(
+            '{{ users|map(attribute="lastname", default="")|join(", ") }}'
+        )
         users = [
             Fullname("john", "lennon"),
             Fullname("jane", "edwards"),
@@ -673,6 +679,8 @@ class TestFilter:
             Firstname("mike"),
         ]
         assert tmpl.render(users=users) == "lennon, edwards, None, smith"
+        assert test_list.render(users=users) == "lennon, edwards, None, ['smith', 'x']"
+        assert test_str.render(users=users) == "lennon, edwards, None, "
 
     def test_simple_select(self, env):
         env = Environment()