service method as twig global variable

前端 未结 4 462
有刺的猬
有刺的猬 2020-12-19 01:52

In my symfony2 application, I have a getPorfolioUser method which return a specific user variable.

I am looking forward to be able to call

{%

相关标签:
4条回答
  • 2020-12-19 02:24

    I was having some difficult with this as well and finally solved it by doing the following:

     

    1. Setup your bundle (e.g: MyVendor/MyBundle)

      $ app/console generate:bundle
      

     

    1. In your bundle directory, create MyService.php class file in the DependencyInjection folder.

     

    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:

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

     

    1. In app/config/config.yml under the twig configuration section

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

     

    1. Then in your twig templates you can reference the variable like so:

       {{ mystuff.example }}
      

     

    DISCLAIMER

    this is just how I got it to work....

    hope this helps.

    0 讨论(0)
  • 2020-12-19 02:29

    When you look here : 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. It is an instance of GlobalVariables.

    GlobalVariables is Symfony\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.

    0 讨论(0)
  • 2020-12-19 02:31

    One approach is use a CONTROLLER event listener. I like to use CONTROLLER instead of REQUEST because it ensures that all the regular request listeners have done their thing already.

    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

    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

    Either way works well.

    0 讨论(0)
  • 2020-12-19 02:34

    You can define your custom service as twig globals variable as follow:

    in the 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

    {% if myGlobaService.portfolio_user() %}
    

    Hope this help

    0 讨论(0)
提交回复
热议问题