8000 fixes for issues mentioned in comments by @weaverryan and @stof · web-dev/symfony-docs@3970ba8 · GitHub
[go: up one dir, main page]

Skip to content

Commit 3970ba8

Browse files
committed
fixes for issues mentioned in comments by @weaverryan and @stof
1 parent 1222fff commit 3970ba8

File tree

1 file changed

+54
-36
lines changed

1 file changed

+54
-36
lines changed

cookbook/templating/twig_extension.rst

Lines changed: 54 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,16 @@
22
single: Twig extensions
33

44
How to write a custom Twig extension
5-
============================================
5+
====================================
66

7-
The main motivation for writing an extension is to move often used code into a reusable class like adding support for internationalization.
8-
An extension can define tags, filters, tests, operators, global variables, functions, and node visitors.
7+
The main motivation for writing an extension is to move often used code
8+
into a reusable class like adding support for internationalization.
9+
An extension can define tags, filters, tests, operators, global variables,
10+
functions, and node visitors.
911

10-
Creating an extension also makes for a better separation of code that is executed at compilation time and code needed at runtime. As such, it makes your code faster.
12+
Creating an extension also makes for a better separation of code that is
13+
executed at compilation time and code needed at runtime. As such, it makes
14+
your code faster.
1115

1216
.. tip::
1317

@@ -17,42 +21,42 @@ Create the extension class
1721
--------------------------
1822

1923
To get your custom functionality you must first create a Twig Extension class.
20-
As an example we will create a price filter to format a given number into price.
24+
As an example we will create a price filter to format a given number into price::
2125

22-
.. code-block:: php
26+
// src/Acme/DemoBundle/Twig/AcmeExtension.php
2327

24-
<?php
25-
26-
// src/Acme/DemoBundle/Twig/AcmeExtension.php
28+
namespace Acme\DemoBundle\Twig;
2729

28-
namespace Acme\DemoBundle\Twig;
30+
use Twig_Extension;
31+
use Twig_Filter_Method;
32+
use Twig_Function_Method;
2933

30-
use Twig_Extension;
31-
use Twig_Filter_Method;
32-
use Twig_Function_Method;
34+
class AcmeExtension extends Twig_Extension
35+
{
36+
public function getFilters()
37+
{
38+
return array(
39+
'price' => new Twig_Filter_Method($this, 'priceFilter'),
40+
);
41+
}
42+
43+
public function priceFilter($number, $decimals = 0, $decPoint = '.', $thousandsSep = ',')
44+
{
45+
$price = number_format($number, $decimals, $decPoint, $thousandsSep);
46+
$price = '$' . $price;
47+
48+
return $price;
49+
}
3350

34-
class AcmeExtension extends Twig_Extension
51+
public function getName()
3552
{
36-
public function getFilters()
37-
{
38-
return array(
39-
'price' => new Twig_Filter_Method($this, 'priceFilter'),
40-
);
41-
}
42-
43-
public function priceFilter($number, $decimals = 0, $decPoint = '.', $thousandsSep = ',')
44-
{
45-
$price = number_format($number, $decimals, $decPoint, $thousandsSep);
46-
$price = '$' . $price;
47-
48-
return $price;
49-
}
50-
51-
public function getName()
52-
{
53-
return 'acme_extension';
54-
}
53+
return 'acme_extension';
5554
}
55+
}
56+
57+
.. tip::
58+
59+
Along with custom filters, you can also add custom `functions` and register `global variables`.
5660

5761
Register extension as a service
5862
-------------------------------
@@ -87,16 +91,30 @@ Now you must let Service Container know about your newly created Twig Extension:
8791
$acmeDefinition = new Definition('\Acme\DemoBundle\Twig\AcmeExtension');
8892
$acmeDefinition->addTag('twig.extension');
8993
$container->setDefinition('acme.twig.acme_extension', $acmeDefinition);
90-
94+
95+
.. note::
96+
97+
Keep in mind that Twig Extensions are not lazily loaded. This means that
98+
there's a higher chance that you'll get a **CircularReferenceException**
99+
or a **ScopeWideningInjectionException** if any services
100+
(or your Twig Extension in this case) are dependent on the request service.
91101

92102
Using the custom extension
93-
---------------------------
103+
--------------------------
94104

95105
Using your newly created Twig Extension is no different than any other:
96106

97107
.. code-block:: html+jinja
98108

99109
{# outputs $5,500.00 #}
100110
{{ '5500' | price }}
111+
112+
Learning further
113+
----------------
114+
115+
For a more in-depth look into Twig Extensions, please take a look at the `Twig extensions documentation`_.
101116

102-
.. _`Twig official extension repository`: http://github.com/fabpot/Twig-extensions
117+
.. _`Twig official extension repository`: http://github.com/fabpot/Twig-extensions
118+
.. _`Twig extensions documentation`: http://twig.sensiolabs.org/doc/extensions.html
119+
.. _`global variables`: http://twig.sensiolabs.org/doc/extensions.html#globals
120+
.. _`functions`: http://twig.sensiolabs.org/doc/extensions.html#functions

0 commit comments

Comments
 (0)
0