我的小枝模板"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"))发布于 2016-03-02 18:34:55
这张海报表明,一根树枝的延伸工作太多了,但这里有一个例子:
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() }}别大惊小怪。没有肌肉;
发布于 2016-03-01 17:23:30
首先,您需要将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访问到小枝模板
{% block notificationD %} {{count}} {% endblock %} 发布于 2016-03-02 11:23:57
您的控制器:
// 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 %} 或者,如果您只想调用特定控制器的操作并在任何树枝模板中呈现它的结果,则可以使用渲染小枝函数。
您的控制器
// 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模板
{{ count }}在其他模板中,您现在可以呈现控制器的操作:
{% block notificationD %} {{ render(controller("AcmeDemoBundle:YourController:getDemandeEnCour")) }} {% endblock %} 有关更多细节,请参见嵌入其他控制器。
https://stackoverflow.com/questions/35729067
复制相似问题