简体   繁体   English

服务方法作为twig全局变量

[英]service method as twig global variable

In my symfony2 application, I have a getPorfolioUser method which return a specific user variable. 在我的symfony2应用程序中,我有一个getPorfolioUser方法,它返回一个特定的用户变量。

I am looking forward to be able to call 我很期待能打电话

{% if portfolio_user %} {%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 ? 这样做的简单方法是什么?

Thanks! 谢谢!

You can define your custom service as twig globals variable as follow: 您可以将自定义服务定义为twig globals variable ,如下所示:

in the config.yml 在config.yml中

# Twig Configuration
twig:
    debug:            "%kernel.debug%"
    strict_variables: "%kernel.debug%"
    globals:
        myGlobaService: "@acme.demo_portfolio_service"  #The id of your service

Use it a Twig file 使用它是一个Twig文件

{% if myGlobaService.portfolio_user() %}

Hope this help 希望这有帮助

One approach is use a CONTROLLER event listener. 一种方法是使用CONTROLLER事件监听器。 I like to use CONTROLLER instead of REQUEST because it ensures that all the regular request listeners have done their thing already. 我喜欢使用CONTROLLER而不是REQUEST,因为它确保所有常规请求监听器已经完成了它们的工作。

use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\HttpKernel\Event\FilterControllerEvent;

use Symfony\Component\EventDispatcher\EventSubscriberInterface;

class ProjectEventListener implements EventSubscriberInterface
{
public static function getSubscribedEvents()
{
    return array
    (
        KernelEvents::CONTROLLER => array(
            array('onControllerProject'),
        ),
    );
}
private $twig;
public function __construct($twig)
{
    $this->twig = $twig;
}
public function onControllerProject(FilterControllerEvent $event)
{
    // Generate your data
    $project = ...;

    // Twig global
    $this->twig->addGlobal('project',$project);    
}

# services.yml
cerad_project__project_event_listener:
    class: ...\ProjectEventListener
    tags:
        - { name: kernel.event_subscriber }
    arguments:
        - '@twig'

Listeners are documented here: http://symfony.com/doc/current/cookbook/service_container/event_listener.html 听众在此处记录: http//symfony.com/doc/current/cookbook/service_container/event_listener.html

Another approach would be to avoid the twig global altogether and just make a twig extension call. 另一种方法是完全避免树枝全球化,只是进行枝条延伸调用。 http://symfony.com/doc/current/cookbook/templating/twig_extension.html http://symfony.com/doc/current/cookbook/templating/twig_extension.html

Either way works well. 无论哪种方式都很好。

When you look here : http://symfony.com/doc/current/reference/twig_reference.html#app 当你看到这里: http//symfony.com/doc/current/reference/twig_reference.html#app

You can read this : 你可以读到这个:

The app variable is available everywhere and gives access to many commonly needed objects and values. app变量随处可用,可以访问许多常用的对象和值。 It is an instance of GlobalVariables. 它是GlobalVariables的一个实例。

GlobalVariables is Symfony\\Bundle\\FrameworkBundle\\Templating\\GlobalVariables GlobalVariablesSymfony\\Bundle\\FrameworkBundle\\Templating\\GlobalVariables

I never do it but I think one way is to overide this class in order to put your special needs in. 我从来没有这样做,但我认为一种方法是覆盖这个课程,以满足你的特殊需求。

I was having some difficult with this as well and finally solved it by doing the following: 我也遇到了一些困难,最后通过以下方式解决了这个问题:

 

  1. Setup your bundle (eg: MyVendor/MyBundle) 设置你的包(例如:MyVendor / MyBundle)

     $ app/console generate:bundle 

 

  1. In your bundle directory, create MyService.php class file in the DependencyInjection folder. 在bundle目录中,在DependencyInjection文件夹中创建MyService.php类文件。

 

  1. In this class file, create the function 在此类文件中,创建该函数

     public function getExample(){ return "it works!!!"; } 

 

  1. In app/config/services.yml create a new service like so: app / config / services.yml中创建一个新服务,如下所示:

     myvendor.mybundle.myservice class: MyVendor\\MyBundle\\DependencyInjection\\MyService 

 

  1. In app/config/config.yml under the twig configuration section 在根据树枝配置部分的应用程序/配置/ config.yml

     twig: globals: mystuff: '@myvendor.mybundle.myservice' 

 

  1. Then in your twig templates you can reference the variable like so: 然后在您的twig模板中,您可以像这样引用变量:

      {{ mystuff.example }} 

 

DISCLAIMER 免责声明

this is just how I got it to work.... 这就是我如何让它工作....

hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM