symfony2.3过滤其他过滤器内的树枝

Bic*_*icu 2 symfony twig

我有树枝过滤器:

class AcmeExtension extends \Twig_Extension
{
    public function getFilters()
    {
        return array(
            new \Twig_SimpleFilter('price', array($this, 'priceFilter')),
        );
    }

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

        return $price;
    }
}
Run Code Online (Sandbox Code Playgroud)

但如何在其他过滤器内调用价格过滤器?在symfony 2.0中声明过滤器 'price' => new \Twig_Filter_Method($this, 'priceFilter')

并且可以从另一个过滤器中调用它.

谢谢,抱歉我的英语

Emi*_*aos 5

如果您希望将其他过滤器的返回值放入价格过滤器中,则可以将它们链接在树枝中:

{{ value|acme_filter|price }}
Run Code Online (Sandbox Code Playgroud)

或者在另一个方向,如果您需要其他过滤器中的价格过滤器的返回值:

{{ value|price|acme_filter }}
Run Code Online (Sandbox Code Playgroud)

如果您真的需要其他过滤器内的价格过滤器,没问题.扩展是一个普通的PHP类.

public function acmeFilter($whatever)
{
    // do something with $whatever

    $priceExtentsion = new PriceExtenstion();
    $whatever = $priceExtension->priceFilter($whatever);

    // do something with $whatever

    return $whatever;
}
Run Code Online (Sandbox Code Playgroud)