本文介紹了服務方法作為樹枝全局變量的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
在我的 symfony2 應用程序中,我有一個 getPorfolioUser 方法,它返回一個特定的用戶變量.
In my symfony2 application, I have a getPorfolioUser method which return a specific user variable.
我期待能夠打電話
{% 如果投資組合用戶 %}
{% if portfolio_user %}
在樹枝上.我不明白如何將其設置為全局變量,因為從文檔中我的印象是我只能設置固定元素或服務,而不能設置服務的方法.
in twig. I did not understand how I could set this as a global variable as from the documentation I am under the impression I can only set fixed elements or services but not services' methods.
我是否有義務為此編寫擴展程序或幫助程序?這樣做的更簡單方法是什么?
Am I obliged to write an extension or a helper for that ? What the simpler way of doing this ?
謝謝!
推薦答案
您可以將自定義服務定義為 twig globals 變量
如下:
You can define your custom service as twig globals variable
as follow:
# Twig Configuration
twig:
debug: "%kernel.debug%"
strict_variables: "%kernel.debug%"
globals:
myGlobaService: "@acme.demo_portfolio_service" #The id of your service
使用 Twig 文件
{% if myGlobaService.portfolio_user() %}
希望對您有所幫助
這篇關于服務方法作為樹枝全局變量的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!