Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access an object method with Twig

Tags:

twig

symfony

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 ?

like image 424
mlwacosmos Avatar asked Mar 07 '13 17:03

mlwacosmos


People also ask

How do you find the current route in Twig?

You can get the current URL in Twig/Silex 2 like this: global. request. attributes. get('_route') .

How do you use variables in Twig?

In Twig templates variables can be accessed using double curly braces notation {{ variableName }} .


1 Answers

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(...) }} 
like image 131
Elnur Abdurrakhimov Avatar answered Oct 01 '22 16:10

Elnur Abdurrakhimov