Truthy Types
When data values are transposed across different data types, can lose true and false meaning. The best example of this is many template frameworks interpret the string “False” to be a logical true value.
A truthiness or falsiness logic check adds additional type-specific checks as a convenience to Recipe chefs in Visual Delivery 3 and beyond!
Specifics
By data types, values and results:
Strings
-
Strings are always ‘trimmed’, which removes the leading and trailing whitespace, and lower-cased.
-
Falsy: (Empty String), “false”, “null”, “none”, “undefined”, “0”, “-“, “()”
-
Anything that’s not falsy is truthy!
Numbers
-
Falsy: 0, 0.0, NaN (Not a Number)
-
Anything that’s not falsy is truth, including negative numbers.
Booleans
-
Falsy : false
-
Truthy : true
Nulls + Undefined
- Always Falsy
Arrays
-
Truthy: Arrays with 1 or more elements
-
Falsy: Arrays with zero elements
Objects
-
Truthy: Objects with 1 or more Key/value Pairs
-
Falsy: Objects with zero Key/value Pairs
Handlebars Helpers (Visual ^3.0.5)
Visual delivery provides four handlebars helpers ‘ifAllTruthy’ ‘ifSomeTruthy’ ‘ifAllFalsy’, and ‘ifSomeFalsy’ that override the included ‘if’ and ‘unless’ helpers.
‘ifTruthy’ is an alias for ‘ifAllTruthy’, and ‘ifFalsy’, is an alias for ‘ifAllFalsy’. These both work like a logical AND.
‘IfSomeTruthy’ or ‘ifSomeFalsy’ work like a logical OR.
The helpers accept 0 to many arguments.
ifAllTruthy - If all of the arguments passed are ‘true’, then the block evaluates. If one of the arguments passed is not true, then they do not evaluate.
ifSomeTruthy - If one or more of the arguments passed are ‘true’, then the block evaluates. If no argments passed, then block does not evaluate.
ifTruthy - All true values
{{#ifTruthy true "yes" 1}}
Evaluated!
{{/ifTruthy}}
Evaluates: Evaluated!
ifAllTruthy - With one False value
{{#ifAllTruthy true "yes" 0}}
This block will not show.
{{/ifAllTruthy}}
Evaluates: (No Content)
ifSomeTruthy - With one False value
{{#ifSomeTruthy true 0}}
Evaluated!
{{/ifSomeTruthy}}
Evaluates: Evaluated!
ifFalsy - All false values
{{#ifFalsy undefined "false" 0}}
All False!
{{/ifFalsy}}
Evaluates: All False!