Skip to content

Commit 6b696c2

Browse files
committed
Merge remote-tracking branch 'origin/2.1' into 2.1
2 parents 473c727 + f6667bb commit 6b696c2

File tree

1 file changed

+18
-8
lines changed

1 file changed

+18
-8
lines changed

cookbook/templating/twig_extension.rst

+18-8
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,18 @@ your code faster.
1515

1616
.. tip::
1717

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`_.
1920

2021
Create the Extension Class
2122
--------------------------
2223

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+
2330
To get your custom functionality you must first create a Twig Extension class.
2431
As an example you'll create a price filter to format a given number into price::
2532

@@ -31,14 +38,14 @@ As an example you'll create a price filter to format a given number into price::
3138
public function getFilters()
3239
{
3340
return array(
34-
'price' => new \Twig_Filter_Method($this, 'priceFilter'),
41+
new \Twig_SimpleFilter('price', array($this, 'priceFilter')),
3542
);
3643
}
3744

3845
public function priceFilter($number, $decimals = 0, $decPoint = '.', $thousandsSep = ',')
3946
{
4047
$price = number_format($number, $decimals, $decPoint, $thousandsSep);
41-
$price = '$' . $price;
48+
$price = '$'.$price;
4249

4350
return $price;
4451
}
@@ -51,7 +58,8 @@ As an example you'll create a price filter to format a given number into price::
5158

5259
.. tip::
5360

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`.
5563

5664
Register an Extension as a Service
5765
----------------------------------
@@ -83,9 +91,9 @@ Now you must let the Service Container know about your newly created Twig Extens
8391
// src/Acme/DemoBundle/Resources/config/services.php
8492
use Symfony\Component\DependencyInjection\Definition;
8593
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');
8997
9098
.. note::
9199

@@ -115,9 +123,11 @@ Passing other arguments to your filter:
115123
Learning further
116124
----------------
117125

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`_.
119128

120129
.. _`Twig official extension repository`: https://github.com/fabpot/Twig-extensions
121130
.. _`Twig extensions documentation`: http://twig.sensiolabs.org/doc/advanced.html#creating-an-extension
122131
.. _`global variables`: http://twig.sensiolabs.org/doc/advanced.html#id1
123132
.. _`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

Comments
 (0)