4

ドキュメントから単純な Twig フィルターを作成しました。

public function getFilters() {

        return array(
            'price' => new \Twig_Filter_Method($this, 'priceFilter'),
        );
    }


    public function priceFilter($number, $decimals = 0, $decPoint = '.', $thousandsSep = ',')
    {
        $price = number_format($number, $decimals, $decPoint, $thousandsSep);
        $price = '$' . $price;

        return $price;
    }

構成に登録されています(そのファイルにはうまく機能する機能があるため):

services:
    sybio.twig_extension:
        class: %sybio.twig_extension.class%
        tags:
            - { name: twig.extension }

しかし、それは機能しませんThe filter "price" does not exist。どうして?

4

2 に答える 2

3

最初に、小枝クラスにこの関数があることを確認することはほとんどありません

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

次に、これをデバッグ用の完全なクラス名に変更してみてください。その後、変更できます

class: %sybio.twig_extension.class%class: Acme\DemoBundle\Twig\AcmeExtension

于 2013-01-26T07:31:37.057 に答える
1

おそらく、私の簡単な例を使用できます。

クラス フィルタ:

namespace Project\Bundle\Twig;

class Price extends \Twig_Extension
{
    public function getFilters()
    {
        return array(
            'price' => new \Twig_Filter_Method($this, 'priceFilter'),
        );
    }

    public function priceFilter($arg)
    {
        return number_format($arg);
    }

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

構成:

services:
    bundle.twig.price:
        class: Project\Bundle\Twig\Price
        tags:
            - { name: twig.extension }
于 2013-01-26T07:47:37.140 に答える