Reputation: 507
i have filter in 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;
}
}
but how can call price filter inside other filter? in symfony 2.0 declared filter with 'price' => new \Twig_Filter_Method($this, 'priceFilter')
and could from within another filter call this.
thanks and sorry for my english
Upvotes: 2
Views: 1396
Reputation: 694
class SomeExtension extends \Twig_Extension {
function getName() {
return 'some_extension';
}
function getFilters() {
return [
new \Twig_SimpleFilter(
'filterOne',
function(\Twig_Environment $env, $input) {
$output = dosmth($input);
$filter2Func = $env->getFilter('filterTwo')->getCallable();
$output = call_user_func($filter2Func, $output);
return $output;
},
['needs_environment' => true]
),
new \Twig_SimpleFilter(
'filterTwo',
function ($input) {
$output = dosmth($input);
return $output;
}
)
];
}
}
Upvotes: 3
Reputation: 10085
if you want the returned value of the other filter into your price filter, you can chain them in twig:
{{ value|acme_filter|price }}
Or in the other direction, if you need the return value of the price filter in your other filter:
{{ value|price|acme_filter }}
If you really need the price filter inside your other filter, no problem. The extension is a plain php class.
public function acmeFilter($whatever)
{
// do something with $whatever
$priceExtentsion = new PriceExtenstion();
$whatever = $priceExtension->priceFilter($whatever);
// do something with $whatever
return $whatever;
}
Upvotes: 6
Reputation: 3178
Your use callback function as "static".
You can define your function as static, or replace to \Twig_Filter_Method($this, 'priceFilter')
Upvotes: 0