Symfony 2 - 枝条延伸

时间:2015-08-06 06:27:37

标签: symfony twig

我正在尝试使用我的服务之一进行枝条扩展。

我得到的错误是:

  

未捕获的异常   '的Symfony \元器件\ DependencyInjection \异常\ InvalidArgumentException'   带有消息“A”标签“条目必须是服务的数组   “tagformater_extension”

我的services.yml:

nobookchoosen_service:
    class: AppBundle\Service\nobookchoosenService
    arguments:
        - @request_stack
        - @router

logger_service:
    class: AppBundle\Service\loggerService
    arguments: ["@doctrine.orm.entity_manager", "@security.context"]

tags_service:
    class:  AppBundle\Service\tagsService
    arguments: [%tags%]

documents_service:
    class:  AppBundle\Service\documentsService
    arguments: ["@tags_service"]

tagformater_extension:
    class: AppBundle\Twig\tagformaterExtension
    arguments: ["@tags_service"]
    public: false
    tags:
        { name: twig.extension }

扩展文件:

<?php
namespace AppBundle\Twig;

class tagformaterExtension extends \Twig_Extension
{

    protected $tagsService;

    public function __construct($tagsService)
    {
        $this->tagsService = $tagsService
    }

    public function getFilters()
    {
        return array(
            new \Twig_SimpleFilter('tag', array($this, 'tagFilter')),
        );
    }

    public function tagFilter($tagname)
    {
        $label = "label label-default";
        if(array_key_exists($tagname, $this->tagsConfigArray))
        {
            if($this->tagsConfigArray[$tag]['label']) $label = $this->tagsService->gettaglabel($tagname);
        }

        return "<SPAN CLASS=\"".$label."\">".$tagname."</A>";
    }

    public function getName()
    {
        return 'tagformater_extension';
    }
}

请告知我做错了什么。

1 个答案:

答案 0 :(得分:4)

标签必须是数组。只需在-

之前加{ name: ...}
tagformater_extension:
    class: AppBundle\Twig\tagformaterExtension
    arguments: ["@tags_service"]
    public: false
    tags:
        - { name: twig.extension }