service method as twig global variable(服务方法作为树枝全局变量)
问题描述
在我的 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() %}
希望对您有所帮助
这篇关于服务方法作为树枝全局变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:服务方法作为树枝全局变量


基础教程推荐
- 如何在 Symfony 和 Doctrine 中实现多对多和一对多? 2022-01-01
- 在 yii2 中迁移时出现异常“找不到驱动程序" 2022-01-01
- 找不到类“AppHttpControllersDB",我也无法使用新模型 2022-01-01
- 在 CakePHP 2.0 中使用 Html Helper 时未定义的变量 2021-01-01
- 如何在 XAMPP 上启用 mysqli? 2021-01-01
- 使用 PDO 转义列名 2021-01-01
- HTTP 与 FTP 上传 2021-01-01
- PHP 守护进程/worker 环境 2022-01-01
- phpmyadmin 错误“#1062 - 密钥 1 的重复条目‘1’" 2022-01-01
- Doctrine 2 - 在多对多关系中记录更改 2022-01-01