@@ -15,11 +15,18 @@ your code faster.
15
15
16
16
.. tip ::
17
17
18
- Before writing your own extensions, have a look at the `Twig official extension repository `_.
18
+ Before writing your own extensions, have a look at the
19
+ `Twig official extension repository `_.
19
20
20
21
Create the Extension Class
21
22
--------------------------
22
23
24
+ .. note ::
25
+
26
+ This cookbook describes how to write a custom Twig extension as of
27
+ Twig 1.12. If you are using an older version, please read
28
+ `Twig extensions documentation legacy `_.
29
+
23
30
To get your custom functionality you must first create a Twig Extension class.
24
31
As an example you'll create a price filter to format a given number into price::
25
32
@@ -31,14 +38,14 @@ As an example you'll create a price filter to format a given number into price::
31
38
public function getFilters()
32
39
{
33
40
return array(
34
- 'price' => new \Twig_Filter_Method ($this, 'priceFilter'),
41
+ new \Twig_SimpleFilter( 'price', array ($this, 'priceFilter') ),
35
42
);
36
43
}
37
44
38
45
public function priceFilter($number, $decimals = 0, $decPoint = '.', $thousandsSep = ',')
39
46
{
40
47
$price = number_format($number, $decimals, $decPoint, $thousandsSep);
41
- $price = '$' . $price;
48
+ $price = '$'. $price;
42
49
43
50
return $price;
44
51
}
@@ -51,7 +58,8 @@ As an example you'll create a price filter to format a given number into price::
51
58
52
59
.. tip ::
53
60
54
- Along with custom filters, you can also add custom `functions ` and register `global variables `.
61
+ Along with custom filters, you can also add custom `functions ` and register
62
+ `global variables `.
55
63
56
64
Register an Extension as a Service
57
65
----------------------------------
@@ -83,9 +91,9 @@ Now you must let the Service Container know about your newly created Twig Extens
83
91
// src/Acme/DemoBundle/Resources/config/services.php
84
92
use Symfony\Component\DependencyInjection\Definition;
85
93
86
- $acmeDefinition = new Definition('\Acme\DemoBundle\Twig\AcmeExtension');
87
- $acmeDefinition->addTag(' twig.extension');
88
- $container->setDefinition('acme. twig.acme_extension', $acmeDefinition );
94
+ $container
95
+ ->register('acme. twig.acme_extension', '\Acme\DemoBundle\Twig\AcmeExtension')
96
+ ->addTag(' twig.extension' );
89
97
90
98
.. note ::
91
99
@@ -115,9 +123,11 @@ Passing other arguments to your filter:
115
123
Learning further
116
124
----------------
117
125
118
- For a more in-depth look into Twig Extensions, please take a look at the `Twig extensions documentation `_.
126
+ For a more in-depth look into Twig Extensions, please take a look at the
127
+ `Twig extensions documentation `_.
119
128
120
129
.. _`Twig official extension repository` : https://github.com/fabpot/Twig-extensions
121
130
.. _`Twig extensions documentation` : http://twig.sensiolabs.org/doc/advanced.html#creating-an-extension
122
131
.. _`global variables` : http://twig.sensiolabs.org/doc/advanced.html#id1
123
132
.. _`functions` : http://twig.sensiolabs.org/doc/advanced.html#id2
133
+ .. _`Twig extensions documentation legacy` : http://twig.sensiolabs.org/doc/advanced_legacy.html#creating-an-extension
0 commit comments