You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{% setposts= get_posts({'post_type': 'not_existing'}) %}
{% ifposts %}
This is false
{% endif %}
{% ifpostsis notempty %}
This is false
{% endif %}
Actual behavior
{% setposts= get_posts({'post_type': 'not_existing'}) %}
{% ifposts %}
This is true
{% endif %}
{% ifpostsis notempty %}
This is false
{% endif %}
Notes
An empty array is falsy in PHP, which allow's Twig's if to tests if an array is empty Doc. This doesn't work, if you have a lazy Post Collection (get_posts) and not an array.
You need to use length or empty to check the Post Collection in Twig.
Solution
I don't see a way to improve this currently as the RFC for falsifable is not implemented - except to return an empty array if the post collection is empty (found_posts == 0).
I suggest updating the docs in the meantime with an example as this is not obvious and can lead to bugs.
What version of Timber are you using?
2.0
What version of WordPress are you using?
6.2
What version of PHP are you using?
8.1
How did you install Timber?
Installed or updated Timber through Composer
The text was updated successfully, but these errors were encountered:
rubas
changed the title
[DOC] Empty post collection is true inside twig if statement
[DOCS] Empty post collection is true inside twig if statement
Feb 7, 2024
rubas
changed the title
[DOCS] Empty post collection is true inside twig if statement
[BUG] Empty post collection is true inside twig if statement
Feb 7, 2024
Expected Behavior
Actual behavior
Notes
An empty array is falsy in PHP, which allow's Twig's
if
to tests if an array is empty Doc. This doesn't work, if you have a lazy Post Collection (get_posts
) and not an array.You need to use
length
orempty
to check the Post Collection in Twig.Solution
I don't see a way to improve this currently as the RFC for
falsifable
is not implemented - except to return an empty array if the post collection is empty (found_posts == 0
).I suggest updating the docs in the meantime with an example as this is not obvious and can lead to bugs.
What version of Timber are you using?
2.0
What version of WordPress are you using?
6.2
What version of PHP are you using?
8.1
How did you install Timber?
Installed or updated Timber through Composer
The text was updated successfully, but these errors were encountered: