when

Picks between two values by the truthiness of the piped one, reading truthiness the way a template `if` does.

It is what lets a flag render as the word it means, so {{ published | when:'live','draft' }} says in one expression what a pair of string replacements said by accident.

Both arguments are required, and either may be a literal or a variable. A missing second argument is a template mistake rather than a shorthand for an empty string, since a choice with one branch is a choice somebody forgot to finish.

A nil value is refused rather than taken as the falsy branch. Absent data is not one of the two cases the template named, and picking a branch for it hides a misspelled path behind an answer that looks deliberate. Put a default ahead of the when to say which branch absence belongs to.

InputArgumentsReturnsanyanyanyany
Source
BasicSource

Renders a flag as the word it means, so a template says what a boolean stands for in one expression.

Given

{"published": true}

Template

{{ published | when:'live','draft' }}

Result

live
FalsySource

Takes the second branch for a value a template `if` would read as false, an empty string included.

Given

{"title": ""}

Template

{{ title | when:'named','untitled' }}

Result

untitled
MissingSource

Shows a miss reaching when as falsey and answered there, so the falsy branch stands in for absent data and nothing downstream sees the miss.

Given

{}

Template

{{ enabled | default:false | when:'on','off' }}

Result

off