Twig Components / Methods / if
Note: You are currently reading the documentation for Bolt 5.0. Looking for the documentation for Bolt 5.2 instead?
if
is a Twig tag to test if an expression evaluates to true. The if statement in Twig is comparable with the if
statements of PHP.
You can also test if an array is not empty:
Note¶
If you want to test if the variable is defined, use if users is defined instead.
You can also use not to check for values that evaluate to false:
For multiple conditions, and and or can be used:
For multiple branches elseif and else can be used like in PHP. You can use more complex expressions there too:
Note¶
The rules to determine if an expression is true or false are the same as in PHP; here are the edge cases rules:
Value | Boolean evaluation |
---|---|
empty string | false |
numeric zero | false |
NAN (Not A Number) | true |
INF (Infinity) | true |
whitespace-only string | true |
string "0" or '0' | false |
empty array | false |
null | false |
non-empty array | true |
object | true |
Source: Twig
Couldn't find what you were looking for? We are happy to help you in the forum, on Slack or on Github.