我正在尝试注册(阅读docs)一个Twig扩展,一切似乎都是正确的,除非在Twig文件中找不到它。
收到以下错误:
The function "getPostCount" does not exist in AcmeDemoBundle:Page:index.html.twig at line 17
有人能告诉我我做错了吗?
services.yml
acme.twig.acme_extension:
class: Acme\DemoBundle\Twig\PostExtension
tags:
- { name: twig. extension }
arguments:
em: "@doctrine.orm.entity_manager"
PostExtension.php
class PostExtension extends \Twig_Extension
{
private $em;
public function __construct(EntityManager $em)
{
$this->em = $em;
}
public function getFilters()
{
return array(
);
}
public function getFunctions()
{
return array(
'getPostCount' => new \Twig_Function_Method($this,'getPostCount')
);
}
public function getPostCount($year, $month)
{
return $this->$em->getRepository('AcmeDemoBundle:Post')
->getPostCountsByMonth($year, $month);
}
public function getName()
{
return 'post_extension';
}
}
枝条
{{ getPostCount('2014', 'July') }}
答案 0 :(得分:2)
在services.yml:
删除twig.extension中的额外空格。
tags:
- { name: twig.extension }