Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

checking if a method exists or can be called in twig

Tags:

html

php

twig

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  %}
like image 202
adit Avatar asked Aug 20 '14 15:08

adit


1 Answers

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

like image 113
JoDev Avatar answered Oct 05 '22 09:10

JoDev