Maintenance method as twig global variable

In my symfony2 application, I have a getPorfolioUser method that returns a specific user variable.

I look forward to calling

{% if portfolio_user%}

in the branch. I did not understand how I can set this as a global variable, since from the documentation I am impressed with, I can only set fixed elements or services, but not service methods.

Do I have to write an extension or an assistant for this? What is an easy way to do this?

Thanks!

+8
php global-variables symfony service twig
source share
4 answers

You can define your user service as twig globals variable as follows:

in config.yml

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

Use Twig File

 {% if myGlobaService.portfolio_user() %} 

Hope for this help

+11
source share

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

 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 described here: http://symfony.com/doc/current/cookbook/service_container/event_listener.html

Another approach would be to avoid the global branch altogether and simply make a call to expand the twig. http://symfony.com/doc/current/cookbook/templating/twig_extension.html

It works well anyway.

+4
source share

When you look here: http://symfony.com/doc/current/reference/twig_reference.html#app

You can read this:

An application variable is available everywhere and provides access to many commonly required objects and values. This is an example of GlobalVariables.

GlobalVariables Symfony\Bundle\FrameworkBundle\Templating\GlobalVariables

I never do this, but I think one way is to override this class to accommodate your special needs.

+1
source share

It was also difficult for me to cope with this and finally solve it by doing the following:

  • Install your kit (e.g. MyVendor / MyBundle)

     $ app/console generate:bundle 

  1. In the directory of your package, create the MyService.php file in the DependencyInjection folder.

  1. In this class file, create a function

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

  1. In app / config / services.yml create a new service, for example:

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

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

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

  1. Then in your templates you can refer to the variable as follows:

      {{ mystuff.example }} 

RENOUNCEMENT

This is exactly how I earned.

hope this helps.

0
source share

All Articles