src/Twig/PageExtension.php line 54

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4.  * Copyright (C) Office National de Publication et de Communication - All Rights Reserved
  5.  *
  6.  * Unauthorized copying of this file, via any medium is strictly prohibited by law
  7.  * This file is proprietary and confidential
  8.  */
  9. namespace App\Twig;
  10. use Twig\Extension\AbstractExtension;
  11. use Twig\TwigFunction;
  12. use App\Entity\Page;
  13. final class PageExtension extends AbstractExtension
  14. {
  15.     /**
  16.      * @var EntityManager
  17.      */
  18.     protected $em;
  19.     public function __construct(\Doctrine\ORM\EntityManagerInterface $entityManager)
  20.     {
  21.         $this->em $entityManager;
  22.     }
  23.     public function getFunctions()
  24.     {
  25.         return [
  26.             new TwigFunction('page', [$this'contenu']),
  27.             new TwigFunction('pageTitre', [$this'title']),
  28.         ];
  29.     }
  30.     public function contenu(string $slug)
  31.     {
  32.         $pageRepo $this->em->getRepository(Page::class);
  33.         $page $pageRepo->findOneBySlug($slug);
  34.         if ( null == $page )
  35.             return null;
  36.         $texte $page->getTexte();
  37.         return $texte;
  38.     }
  39.     public function title(string $slug)
  40.     {
  41.         $pageRepo $this->em->getRepository(Page::class);
  42.         $page $pageRepo->findOneBySlug($slug);
  43.         if ( null == $page ){
  44.             return "";
  45.         };
  46.         $texte $page->getTitre();
  47.         return $texte;
  48.     }
  49. }