Access an object method with Twig

mlwacosmos picture mlwacosmos · Mar 7, 2013 · Viewed 37.4k times · Source

I have a symfony controller returning this:

return $this->render('MyBundle:Default:index.html.twig', array('menu' => $menu));

menu is a Menu object.

In my template I want to call a method from the Menu class:

getHTML(string s1, String s2, array tab) 

returning a HTML string.

How do I do that in the template? Is it even possible ?

Answer

Elnur Abdurrakhimov picture Elnur Abdurrakhimov · Mar 7, 2013

Yea, it's possible:

{{ menu.getHTML('first-string', 'second-string', ['tab1', 'tab2']) }}

Since Twig handles getters and issers automatically, you can omit the get part:

{{ menu.HTML(...) }}