Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PHP

twig global

<?php

namespace AppTwig;

use DoctrineORMEntityManager;
use DoctrineORMEntityManagerInterface;
use SymfonyComponentEventDispatcherEventSubscriberInterface;
use SymfonyComponentHttpKernelKernelEvents;
use TwigEnvironment;
use DoctrineCommonCollectionsCriteria;

class TwigGlobalSubscriber implements EventSubscriberInterface
{

    private $twig, $em;

    public function __construct(Environment $twig, EntityManagerInterface $em)
    {
        $this->twig = $twig;
        $this->em = $em;
    }

    public function injectGlobalVariables()
    {
        $tab = [];
        foreach ($this->em->getRepository('App:Parametres')->findAll() as $parametre) {
            $tab[$parametre->getNom()] = $parametre->getValeur();
        }
        $this->twig->addGlobal('parametres', $tab);
    }

    public static function getSubscribedEvents()
    {
        return [KernelEvents::CONTROLLER =>  'injectGlobalVariables'];
    }

    public function onKernelRequest()
    {
    }
}

and in services.yml
      AppTwigTwigGlobalSubscriber:
        tags:
            - { name: kernel.event_listener, event: kernel.request }

  
 
PREVIOUS NEXT
Tagged: #twig #global
ADD COMMENT
Topic
Name
2+3 =