Replies: 1 comment 1 reply
-
This is explicitly documented: https://twig.symfony.com/doc/3.x/tests/empty.html
The rule is that That's one of the reason why |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
First of all: Why is there only a "Q&A" section? I hope it's okay if I share a little "cheat sheet" I've created. At least I've also seen other posts like this. So, here I go:
I've created a table that helps you decide what technique you should use to test a variable and what the results are.
null""0[]false"x"1[1]truevalval == trueval == falseval == nullval is nullval is emptyval is definedval ?? ...default(...)filtertrue, returns the LHS of??, or doesn't trigger thedefaultfilterfalse, returns the RHS of??, or does trigger thedefaultfilterThe first test (
val) means using just the value itself as a test like in{% if val %}or{{ val ? 'yes' : 'no' }}.There are a few things that are noteworthy:
falseis considered "empty" which I didn't expectdefaultfilter triggers forfalsevalues, which means it cannot be used to provide a default value for bool options. Like, if youembedorincludea template that provides an boolean option parameter you cannot set a default value using thedefaultfilter in that template, like:{% set someOption = someOption|default(true) %}. If you pass in... with { someOption: false }it gets defaulted totrue.I hope it's helpful for some of you.
Beta Was this translation helpful? Give feedback.
All reactions