<?php
declare(strict_types=1);
/*
* Copyright (C) Office National de Publication et de Communication - All Rights Reserved
*
* Unauthorized copying of this file, via any medium is strictly prohibited by law
* This file is proprietary and confidential
*/
namespace App\Twig;
use Twig\Extension\AbstractExtension;
use Twig\TwigFunction;
use App\Entity\Page;
final class PageExtension extends AbstractExtension
{
/**
* @var EntityManager
*/
protected $em;
public function __construct(\Doctrine\ORM\EntityManagerInterface $entityManager)
{
$this->em = $entityManager;
}
public function getFunctions()
{
return [
new TwigFunction('page', [$this, 'contenu']),
new TwigFunction('pageTitre', [$this, 'title']),
];
}
public function contenu(string $slug)
{
$pageRepo = $this->em->getRepository(Page::class);
$page = $pageRepo->findOneBySlug($slug);
if ( null == $page )
return null;
$texte = $page->getTexte();
return $texte;
}
public function title(string $slug)
{
$pageRepo = $this->em->getRepository(Page::class);
$page = $pageRepo->findOneBySlug($slug);
if ( null == $page ){
return "";
};
$texte = $page->getTitre();
return $texte;
}
}