使用 Twig 访问对象方法 [英] Access an object method with Twig

查看:25
本文介绍了使用 Twig 访问对象方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 symfony 控制器返回这个:

I have a symfony controller returning this:

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

menu 是一个 Menu 对象.

在我的模板中,我想从 Menu 类中调用一个方法:

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

getHTML(string s1, String s2, array tab) 

返回一个 HTML 字符串.

returning a HTML string.

我如何在模板中做到这一点?甚至有可能吗?

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

推荐答案

是的,这是可能的:

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

由于 Twig 自动处理 getter 和 issers,您可以省略 get 部分:

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

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

这篇关于使用 Twig 访问对象方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆