I use Twig extension to pass global variables from the database like in the code below. But I want to make this more dynamic to get data from database by the id
parameter ..
service
app.twig.database_globals_extension:
class: Coursat\CoursatBundle\Twig\Extension\DatabaseGlobalsExtension
arguments: ["@doctrine.orm.entity_manager"]
tags:
- { name: twig.extension }
extension
<?php
namespace Coursat\CoursatBundle\Twig\Extension;
use Doctrine\ORM\EntityManager;
class DatabaseGlobalsExtension extends \Twig_Extension
{
protected $em;
public function __construct(EntityManager $em)
{
$this->em = $em;
}
public function getGlobals()
{
return array (
"myVariable" => $this->em->getRepository('CoursatBundle:test')->find(##I want to pass a var here from the template##),
);
}
public function getName()
{
return "CoursatBundle:DatabaseGlobalsExtension";
}
}
template
{{ myVariable.name() }}