【问题标题】:Filter not exit in my custom Twig extension过滤器未在我的自定义 Twig 扩展中退出
【发布时间】:2015-01-26 23:01:29
【问题描述】:

关注http://symfony.com/doc/current/cookbook/templating/twig_extension.html 创建我的自定义 Twig 过滤器,用于我的 Symfony 2 项目。

一切似乎都很好,但是在加载页面时,它说:

The filter "tss" does not exist in AppBundle:Default:status.html.twig at line 7

我的services.yml

services:
    app.twig_extension:
        class: AppBundle\Twig\AppExtension
        tags:
            - { name: twig.extension }

我的src/AppBundle/Twig/AppExtension.php

<?php
namespace AppBundle\Twig;

class AppExtension extends \Twig_Extension
{
    public function getFilter()
    {
        return [
            new \Twig_SimpleFilter('tss', [$this, 'tssFilter']),
        ];
    }

    public function tssFilter(\DateTime $timestamp)
    {
        return 'ready';
    }

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

我错过了什么吗?

感谢您的建议。

【问题讨论】:

    标签: php symfony filter twig


    【解决方案1】:

    您拼错了方法名称。它的public function getFilters() 不是public function getFilter()

    【讨论】:

    • 很好,只要你有the eye 8-),就可以轻松加分。
    • 是的,同意。我的 IDE 没有提示我在继承的接口中实现该功能。
    猜你喜欢
    • 1970-01-01
    • 2014-03-09
    • 1970-01-01
    • 2014-12-01
    • 1970-01-01
    • 2015-09-11
    • 2020-03-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多