@@ -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 array(
37
- new TwigFilter('price', array($this, 'priceFilter ')),
37
+ new TwigFilter('price', array($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;
@@ -55,9 +55,32 @@ Create a class that extends ``AbstractExtension`` and fill in the logic::
55
55
versions before 1.26, include this method which is omitted in the example
56
56
above.
57
57
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
+
58
81
.. tip ::
59
82
60
- Along with custom filters, you can also add custom ` functions `_ and register
83
+ Along with custom filters and functions , you can also register
61
84
`global variables `_.
62
85
63
86
Register an Extension as a Service
0 commit comments