@@ -34,11 +34,11 @@ Create a class that extends ``AbstractExtension`` and fill in the logic::
34
34
public function getFilters()
35
35
{
36
36
return [
37
- new TwigFilter('price', [$this, 'priceFilter ']),
37
+ new TwigFilter('price', [$this, 'formatPrice ']),
38
38
];
39
39
}
40
40
41
- public function priceFilter ($number, $decimals = 0, $decPoint = '.', $thousandsSep = ',')
41
+ public function formatPrice ($number, $decimals = 0, $decPoint = '.', $thousandsSep = ',')
42
42
{
43
43
$price = number_format($number, $decimals, $decPoint, $thousandsSep);
44
44
$price = '$'.$price;
@@ -111,7 +111,7 @@ be significant.
111
111
112
112
That's why Twig allows to decouple the extension definition from its
113
113
implementation. Following the same example as before, the first change would be
114
- to remove the ``priceFilter () `` method from the extension and update the PHP
114
+ to remove the ``formatPrice () `` method from the extension and update the PHP
115
115
callable defined in ``getFilters() ``::
116
116
117
117
// src/AppBundle/Twig/AppExtension.php
@@ -127,14 +127,14 @@ callable defined in ``getFilters()``::
127
127
{
128
128
return [
129
129
// the logic of this filter is now implemented in a different class
130
- new TwigFilter('price', [AppRuntime::class, 'priceFilter ']),
130
+ new TwigFilter('price', [AppRuntime::class, 'formatPrice ']),
131
131
];
132
132
}
133
133
}
134
134
135
135
Then, create the new ``AppRuntime `` class (it's not required but these classes
136
136
are suffixed with ``Runtime `` by convention) and include the logic of the
137
- previous ``priceFilter () `` method::
137
+ previous ``formatPrice () `` method::
138
138
139
139
// src/AppBundle/Twig/AppRuntime.php
140
140
namespace AppBundle\Twig;
@@ -149,7 +149,7 @@ previous ``priceFilter()`` method::
149
149
// extensions, you'll need to inject services using this constructor
150
150
}
151
151
152
- public function priceFilter ($number, $decimals = 0, $decPoint = '.', $thousandsSep = ',')
152
+ public function formatPrice ($number, $decimals = 0, $decPoint = '.', $thousandsSep = ',')
153
153
{
154
154
$price = number_format($number, $decimals, $decPoint, $thousandsSep);
155
155
$price = '$'.$price;
0 commit comments