If inside a twig I have an object that I wanted to be tested if a method called getName can be called on that object, is there such case?
I tried doing the following without any success:
{% if lastCategory.method(getName) is defined %}
Yes, you can do it this way :
{{ attribute(object, method) is defined ? 'Method exists' : 'Method does not exist' }}
In your case, it will be something like
{{ attribute(lastCategory, 'getName') is defined ? 'Method exists' : 'Method does not exist' }}
See the documentation
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With