Fixed template empty() does not match php empty() behaviour

Xon

Well-known member
Affected version
2.0.4
This is something of a feature request but also an unexpected behaviour.

In php*, when empty() is passed a variable expression (ie $foo->bar) it calls isset() before evaluating if the value is falsey.

Template empty() just evaluates if an expression's value is falsey

This is important as XenForo will trigger errors when accessing an entity property which does not exist, where empty would cause __isset to be called on the entity.


*Earlier php version only handle variable expressions (ie empty($foo->bar)), php 7+ handles expressions (ie empty(trim($foo->bar))).
 
Last edited:
The primary use case and aim for the Templater testEmpty method is not to mirror the functionality of the native empty function. Admittedly, that does mean that, potentially, it could have been given a better name. But its primary usage, rather than the <xf:if is="empty($foo.bar)"> usage is actually: <xf:if is="$collection is not empty">.

That said, with the way that the code is structured, it's actually possible for the is / is not empty syntax to call a Templater test while empty() calls a function.

Although changing the testEmpty method to check native empty() might be ok, I'm reluctant to change its behaviour at this point. Instead, we're modifying the code so that there is a new templater function, also called empty() which simply does a native empty() call.
 
Top Bottom