EmptyInterface#4793
Conversation
|
I'm not convinced we should introduce this extension point in Twig core, especially when the craftcms issue says that the clean solution for their problem would actually be to use the |
|
I disagree. This is a super simple change in Twig, and a super easy feature for platforms like Craft, Django, etc., to implement, that would eliminate a lot of rakes that are currently being stepped on. |
|
Alternatively, maybe just change the order of the checks? If |
In most cases, yes – but IMO, if Twig can help avoid performance pitfalls with the |
@brandonkelly moving the order of checks around would probably be a breaking change for values where usage depends on the current order. |
Being more intuitive is actually a fallacy.
This new interface does not guarantee avoiding performance pitfalls (it could actually create them depending on what the implementation does). What it guarantees is that it becomes impossible to document the behavior of the If you know the kind of values you have in a variable, you could use dedicated methods on those objects when relevant instead of using |
Every extension point has the possibility of impacting performance depending on the implementation. In this case there is already a clear performance pitfall, and an opportunity to drastically improve it. Seems like a pretty safe bet to me.
Not to show off, but here’s a stab at how it could be handled in the docs, based on the existing documentation around
|
Adds a new
EmptyInterfacethat any object could implement, giving it control over whatCoreExtension::testEmpty()returns for it.For example, if an object’s simple existence should be counted as not empty, it could do:
This will help with issues like craftcms/cms#18727, where
emptytests and thedefaultfilter can cause performance issues simply by traversing over all the existing properties (many of which may cause DB queries, etc.).