Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

firstof filter (return the first true value of a list) #42

Closed
wants to merge 2 commits into from

3 participants

@Alexis-D

The firstof filter return the first element that is evaluated to True in
a sequence, it return '' if all elements are evaluated to False.

How to use ?

{{ ['hello', baz]|firstof }} -> 'hello'
{{ [0, False]|firstof }} -> ''
{{ [0, False, foo]|firstof }} with foo='bar' -> 'bar'

It's inspired by django firstof:
https://docs.djangoproject.com/en/dev/ref/templates/builtins/?from=olddocs#firstof

Alexis-D added some commits
@Alexis-D Alexis-D Add a firstof filter.
The firstof filter return the first element that is evaluated to True in
a sequence, it return '' if all elements are evaluated to False.

How to use ?
============

{{ ['hello', baz]|firstof }} -> 'hello'
{{ [0, False]|firstof }} -> ''
{{ [0, False, foo]|firstof }} with foo='bar' -> 'bar'

It's inspired by django firstof:
https://docs.djangoproject.com/en/dev/ref/templates/builtins/?from=olddocs#firstof
c041963
@Alexis-D Alexis-D Fix style to match pocoo guidelines. eedfc45
@snoack

In my opinion that use case is not common enough that it is worth to get merged into upstream. A more flexible solution that can satisfy that use case among others, is the filter filter, I have added in my branch better-filters.

{{ [0, False, foo]|filter|first }}
@mitsuhiko mitsuhiko closed this
@axil axil referenced this pull request
Open

firstof function #429

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jul 1, 2011
  1. @Alexis-D

    Add a firstof filter.

    Alexis-D authored
    The firstof filter return the first element that is evaluated to True in
    a sequence, it return '' if all elements are evaluated to False.
    
    How to use ?
    ============
    
    {{ ['hello', baz]|firstof }} -> 'hello'
    {{ [0, False]|firstof }} -> ''
    {{ [0, False, foo]|firstof }} with foo='bar' -> 'bar'
    
    It's inspired by django firstof:
    https://docs.djangoproject.com/en/dev/ref/templates/builtins/?from=olddocs#firstof
  2. @Alexis-D
This page is out of date. Refresh to see the latest.
Showing with 23 additions and 0 deletions.
  1. +14 −0 jinja2/filters.py
  2. +9 −0 jinja2/testsuite/filters.py
View
14 jinja2/filters.py
@@ -320,6 +320,19 @@ def do_first(environment, seq):
@environmentfilter
+def do_firstof(environment, seq):
+ """Return the first "True" item of a sequence.
+ Return '' if all values are evaluated to False."""
+ if not len(seq):
+ return environment.undefined('No first "True" item, '
+ 'sequence was empty')
+ for item in iter(seq):
+ if item:
+ return item
+ return ''
+
+
+@environmentfilter
def do_last(environment, seq):
"""Return the last item of a sequence."""
try:
@@ -775,6 +788,7 @@ def do_attr(environment, obj, name):
'title': do_title,
'capitalize': do_capitalize,
'first': do_first,
+ 'firstof': do_firstof,
'last': do_last,
'random': do_random,
'filesizeformat': do_filesizeformat,
View
9 jinja2/testsuite/filters.py
@@ -94,6 +94,15 @@ def test_first(self):
out = tmpl.render(foo=range(10))
assert out == '0'
+ def test_firstof(self):
+ tmpl = env.from_string('{{ ["hello", False]|firstof }}')
+ out = tmpl.render()
+ assert out == 'hello'
+
+ tmpl = env.from_string('{{ ["", False, 0, None, foo, bar, baz]|firstof }}')
+ out = tmpl.render(foo=False, baz='default')
+ assert out == 'default'
+
def test_float(self):
tmpl = env.from_string('{{ "42"|float }}|'
'{{ "ajsghasjgd"|float }}|'
Something went wrong with that request. Please try again.