Search
 
SCRIPT & CODE EXAMPLE
 

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 }

  
Comment

PREVIOUS NEXT
Code Example
Php :: laravel components scripts 
Php :: order table in laravel 
Php :: df/mpdf/src/Cache.php on line 21 
Php :: faire un fichier zip en php 
Php :: acf blocks register block with enqueue script 
Php :: PHP (php 7.3.5) sample 
Php :: use app http models in laravel 8 
Php :: required if null / require without laravel 
Php :: 0 == "string" php 
Php :: Lumen framework promise 
Php :: curl_setopt timeout php 
Php :: cashier mollie 
Php :: Converting hiec to jpg using javascript before uploading in PHP 
Php :: kinsta check environment 
Php :: Add Spatie provider to providers 
Php :: php know if city exist gmap api 
Php :: ubuntu where are laravel logs stored 
Php :: OroCRM Custom Bundle is loaded? 
Php :: what returns livewire mount 
Php :: siteurl 
Php :: old codestar radio field 
Php :: provenienza geografica di un utente php webmaster 
Php :: check input value is not empty or spaced php 
Php :: laravel where has relation 
Php :: laravel api routes 
Php :: stop php execution with javascript 
Php :: search php array 
Php :: array_shift in php 
Php :: read file and convert each line in array php 
Php :: enable cors cakephp 
ADD CONTENT
Topic
Content
Source link
Name
2+5 =