Amine Harbaoui
Amine Harbaoui

Reputation: 1295

Symfony2 - get the return of controller in twig template

my twig template "backoffice.html.twig" is on Egov/AdminBundle and extends baseBO.html.twig

it contains this block

{% block notificationD %}  {% endblock %}

and in Egov/PosteBundle/Controller/CcpAdminController.php i have this function

public function getDemandeEnCourAction()
{
    $repo = $this   ->getDoctrine()
        ->getManager()
        ->getRepository('EgovCoreBundle:DemandeCCP');

    $qb = $repo->createQueryBuilder('d');
    $qb->select('COUNT(d)');
    $qb->where('d.statut  = :statut');
    $qb->setParameter('statut', 'en cour');
    $count = $qb->getQuery()->getSingleScalarResult();
    return $this->render('@EgovAdmin/Default/backoffice.html.twig', array(
        'count' => (int) $count,
    ));
}

So when i do that

{% block notificationD %} {{ count }} {% endblock %}

i have this exception :

Variable "count" does not exist in @EgovAdmin/Default/backoffice.html.twig 

and if i use render controller like this nothing to change :

render(controller("EgovPosteBundle:CcpAdmin:getDemandeEnCour"))

Upvotes: 0

Views: 1546

Answers (3)

Cerad
Cerad

Reputation: 48865

The poster indicated that a twig extension was too much work but here is an example anyways:

class MyExtension extends \Twig_Extension
{
  private $em;
  public function __construct($em) 
  {
    $this->em = $em;
  }
  public function getFunctions()
  {
    return [            
      new \Twig_SimpleFunction('demande_count',[$this, 'getDemandeCount']),
    ];
  }
  public function getDemandeCount()
  {
    $repo = $this->em->getRepository('EgovCoreBundle:DemandeCCP');

    $qb = $repo->createQueryBuilder('d');
    $qb->select('COUNT(d)');
    $qb->where('d.statut  = :statut');
    $qb->setParameter('statut', 'en cour');
    return $qb->getQuery()->getSingleScalarResult();
  }

Define as a service:

services:
  demande_count:
    class: MyExtension
    arguments: ['@doctrine.orm.default_entity_manager']
    tags: [{ name: twig.extension }]

Then use it in whatever template needs it with:

{{ demande_count() }}

No fuss. No muss;

Upvotes: 3

takeit
takeit

Reputation: 4081

Your Controller:

// AcmeDemoBundle:YourController:getDemandeEnCour

/**
 * @Route("/test")
 */
public function getDemandeEnCourAction()
{
    $repo = $this->getDoctrine()
        ->getManager()
        ->getRepository('EgovCoreBundle:DemandeCCP');

    $qb = $repo->createQueryBuilder('d');
    $qb->select('COUNT(d)');
    $qb->where('d.statut = :statut');
    $qb->setParameter('statut', 'en cour');
    $count = $qb->getQuery()->getSingleScalarResult();

    return $this->render('AcmeDemoBundle:YourController:count.html.twig', array(
      'count' => (int) $count,
    ));
}

Your template:

{% block notificationD %} {{count}} {% endblock %} 

Or you can use render twig function if you want to call only specific controller's action and render it's result in any twig template.

Your Controller:

// AcmeDemoBundle:YourController:getDemandeEnCour

public function getDemandeEnCourAction()
{
    $repo = $this->getDoctrine()
        ->getManager()
        ->getRepository('EgovCoreBundle:DemandeCCP');

    $qb = $repo->createQueryBuilder('d');
    $qb->select('COUNT(d)');
    $qb->where('d.statut = :statut');
    $qb->setParameter('statut', 'en cour');
    $count = $qb->getQuery()->getSingleScalarResult();

    return $this->render('AcmeDemoBundle:YourController:count.html.twig', array(
      'count' => (int) $count,
    ));
}

AcmeDemoBundle:YourController:count.html.twig template:

{{ count }}

In other templates you can now render controller's action:

{% block notificationD %} {{ render(controller("AcmeDemoBundle:YourController:getDemandeEnCour")) }} {% endblock %} 

See also Embedding other Controllers for more details.

Upvotes: 0

CodeIsLife
CodeIsLife

Reputation: 1245

first you need to inject the var into your twig template

/**
 * @Route("/test", name="test")
 * @Template("target.html.twig")
 */
public function getDemandeEnCourAction()
{
    $repo = $this   ->getDoctrine()
        ->getManager()
        ->getRepository('EgovCoreBundle:DemandeCCP');

    $qb = $repo->createQueryBuilder('d');
    $qb->select('COUNT(d)');
    $qb->where('d.statut  = :statut');
    $qb->setParameter('statut', 'en cour');
    $count = $qb->getQuery()->getSingleScalarResult();
    return array( 'count' => $count);
}

and access var into twig template

{% block notificationD %} {{count}} {% endblock %} 

Upvotes: 0

Related Questions