@@ -34,11 +34,11 @@ Create a class that extends ``AbstractExtension`` and fill in the logic::
3434 public function getFilters()
3535 {
3636 return array(
37- new TwigFilter('price', array($this, 'priceFilter ')),
37+ new TwigFilter('price', array($this, 'formatPrice ')),
3838 );
3939 }
4040
41- public function priceFilter ($number, $decimals = 0, $decPoint = '.', $thousandsSep = ',')
41+ public function formatPrice ($number, $decimals = 0, $decPoint = '.', $thousandsSep = ',')
4242 {
4343 $price = number_format($number, $decimals, $decPoint, $thousandsSep);
4444 $price = '$'.$price;
@@ -55,9 +55,32 @@ Create a class that extends ``AbstractExtension`` and fill in the logic::
5555 versions before 1.26, include this method which is omitted in the example
5656 above.
5757
58+ Here's how to create a custom **function **::
59+
60+ // src/AppBundle/Twig/AppExtension.php
61+ namespace AppBundle\Twig;
62+
63+ use Twig\Extension\AbstractExtension;
64+ use Twig\TwigFunction;
65+
66+ class AppExtension extends AbstractExtension
67+ {
68+ public function getFunctions()
69+ {
70+ return array(
71+ new TwigFunction('area', array($this, 'calculateArea')),
72+ );
73+ }
74+
75+ public function calculateArea(int $width, int $length)
76+ {
77+ return $width * $length;
78+ }
79+ }
80+
5881.. tip ::
5982
60- Along with custom filters, you can also add custom ` functions `_ and register
83+ Along with custom filters and functions , you can also register
6184 `global variables `_.
6285
6386Register an Extension as a Service
0 commit comments