- Int and float literals can be written with the '_' separator for
legibility, like 12_345. :pr:`923`
- Fix a bug causing deadlocks in ``LRUCache.setdefault``. :pr:`1000`
+- The ``trim`` filter takes an optional string of characters to trim.
+ :pr:`828`
Version 2.10.2
return soft_unicode(value) % (kwargs or args)
-def do_trim(value):
- """Strip leading and trailing whitespace."""
- return soft_unicode(value).strip()
+def do_trim(value, chars=None):
+ """Strip leading and trailing characters, by default whitespace."""
+ return soft_unicode(value).strip(chars)
def do_striptags(value):
out = tmpl.render()
assert out == '<">&'
+ @pytest.mark.parametrize(
+ ("chars", "expect"), [(None, "..stays.."), (".", " ..stays"), (" .", "stays")]
+ )
+ def test_trim(self, env, chars, expect):
+ tmpl = env.from_string("{{ foo|trim(chars) }}")
+ out = tmpl.render(foo=" ..stays..", chars=chars)
+ assert out == expect
+
def test_striptags(self, env):
tmpl = env.from_string('''{{ foo|striptags }}''')
out = tmpl.render(foo=' <p>just a small \n <a href="#">'