@@ -41,11 +41,11 @@ As an example you'll create a price filter to format a given number into price::
41
41
public function getFilters()
42
42
{
43
43
return array(
44
- new TwigFilter('price', array($this, 'priceFilter ')),
44
+ new TwigFilter('price', array($this, 'formatPrice ')),
45
45
);
46
46
}
47
47
48
- public function priceFilter ($number, $decimals = 0, $decPoint = '.', $thousandsSep = ',')
48
+ public function formatPrice ($number, $decimals = 0, $decPoint = '.', $thousandsSep = ',')
49
49
{
50
50
$price = number_format($number, $decimals, $decPoint, $thousandsSep);
51
51
$price = '$'.$price;
@@ -62,9 +62,32 @@ As an example you'll create a price filter to format a given number into price::
62
62
versions before 1.26, include this method which is omitted in the example
63
63
above.
64
64
65
+ Here's how to create a custom **function **::
66
+
67
+ // src/AppBundle/Twig/AppExtension.php
68
+ namespace AppBundle\Twig;
69
+
70
+ use Twig\Extension\AbstractExtension;
71
+ use Twig\TwigFunction;
72
+
73
+ class AppExtension extends AbstractExtension
74
+ {
75
+ public function getFunctions()
76
+ {
77
+ return array(
78
+ new TwigFunction('total', array($this, 'calculateArea')),
79
+ );
80
+ }
81
+
82
+ public function calculateArea(int $width, int $length)
83
+ {
84
+ return $width * $length;
85
+ }
86
+ }
87
+
65
88
.. tip ::
66
89
67
- Along with custom filters, you can also add custom ` functions `_ and register
90
+ Along with custom filters and functions , you can also register
68
91
`global variables `_.
69
92
70
93
Register an Extension as a Service
0 commit comments