symfony2:如何从模板访问服务 [英] symfony2: how to access service from template

查看:78
本文介绍了symfony2:如何从模板访问服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我创建了服务,是否可以从twig访问服务,而无需创建twig.extension?

If I created a service is there a way to access it from twig, without creating a twig.extension?

推荐答案

您可以在config.yml中将服务设置为树枝全局变量,例如

You can set the service a twig global variable in config.yml, e.g

#app/config/config.yml
twig:
    globals:
        your_service: "@your_service"

在您的template.html.twig文件中,您可以通过以下方式调用服务:

And in your template.html.twig file you can invoke your service this way:

{{ your_service.someMethod(twig_variable) }}

请参见此处.

这篇关于symfony2:如何从模板访问服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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