Skip to content

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

Closed
Alexis-D wants to merge 2 commits intopallets:masterfrom
Alexis-D:master
Closed

firstof filter (return the first true value of a list)#42
Alexis-D wants to merge 2 commits intopallets:masterfrom
Alexis-D:master

Conversation

@Alexis-D
Copy link
Copy Markdown

@Alexis-D 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 2 commits July 1, 2011 17:26
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
@snoack
Copy link
Copy Markdown
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 mentioned this pull request Mar 23, 2015
@github-actions github-actions Bot locked as resolved and limited conversation to collaborators Nov 13, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants