Symfony2 - 在twig模板中获取控制器的返回

时间:2016-03-01 17:09:59

标签: symfony symfony-2.6

我的twig模板" backoffice.html.twig" 位于 Egov / AdminBundle 并扩展 baseBO.html.twig

它包含此块

{% block notificationD %}  {% endblock %}

并在 Egov / PosteBundle / Controller / CcpAdminController.php 我有这个功能

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,
    ));
}

所以当我这样做时

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

我有这个例外:

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

如果我像这样使用渲染控制器,则无需更改:

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

3 个答案:

答案 0 :(得分:3)

海报表明,枝条扩展工作太多,但无论如何这是一个例子:

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();
  }

定义为服务:

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

然后在任何需要它的模板中使用它:

{{ demande_count() }}

不用大惊小怪。没有麻烦;

答案 1 :(得分:0)

首先,您需要将var注入您的树枝模板

/**
 * @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);
}

并将var访问到twig模板

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

答案 2 :(得分:0)

您的控制器:

// 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,
    ));
}

您的模板:

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

如果您只想调用特定控制器的操作并在任何树枝模板中呈现结果,您可以使用render twig功能。

您的控制器

// 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 }}

在其他模板中,您现在可以呈现控制器的操作:

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

有关详细信息,另请参阅Embedding other Controllers