Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

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

Closed
wants to merge 2 commits into
from

Conversation

Projects
None yet
3 participants

Alexis-D commented Jul 1, 2011

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 Jul 1, 2011

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
Contributor

snoack commented Oct 27, 2011

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 May 19, 2013

@axil axil referenced this pull request Mar 23, 2015

Closed

firstof function #429

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment