8000 added cookbook entry on using Twig Extension · web-dev/symfony-docs@1222fff · GitHub
[go: up one dir, main page]

Skip to content

Commit 1222fff

Browse files
committed
added cookbook entry on using Twig Extension
1 parent 0434394 commit 1222fff

File tree

1 file changed

+102
-0
lines changed

1 file changed

+102
-0
lines changed
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
.. index::
2+
single: Twig extensions
3+
4+
How to write a custom Twig extension
5+
============================================
6+
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.
9+
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.
11+
12+
.. tip::
13+
14+
Before writing your own extensions, have a look at the `Twig official extension repository`_.
15+
16+
Create the extension class
17+
--------------------------
18+
19+
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.
21+
22+
.. code-block:: php
23+
24+
<?php
25+
26+
// src/Acme/DemoBundle/Twig/AcmeExtension.php
27+
28+
namespace Acme\DemoBundle\Twig;
29+
30+
use Twig_Extension;
31+
use Twig_Filter_Method;
32+
use Twig_Function_Method;
33+
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+
}
50+
51+
public function getName()
52+
{
53+
return 'acme_extension';
54+
}
55+
}
56+
57+
Register extension as a service
58+
-------------------------------
59+
60+
Now you must let Service Container know about your newly created Twig Extension:
61+
62+
.. configuration-block::
63+
64+
.. code-block:: xml
65+
66+
<!-- src/Acme/DemoBundle/Resources/config/services.xml -->
67+
<services>
68+
<service id="acme.twig.acme_extension" class="Acme\DemoBundle\Twig\AcmeExtension">
69+
<tag name="twig.extension" />
70+
</service>
71+
</services>
72+
73+
.. code-block:: yaml
74+
75+
# src/Acme/DemoBundle/Resources/config/services.yml
76+
services:
77+
acme.twig.acme_extension:
78+
class: Acme\DemoBundle\Twig\AcmeExtension
79+
tags:
80+
- { name: twig.extension }
81+
82+
.. code-block:: php
83+
84+
// src/Acme/DemoBundle/Resources/config/services.php
85+
use Symfony\Component\DependencyInjection\Definition;
86+
87+
$acmeDefinition = new Definition('\Acme\DemoBundle\Twig\AcmeExtension');
88+
$acmeDefinition->addTag('twig.extension');
89+
$container->setDefinition('acme.twig.acme_extension', $acmeDefinition);
90+
91+
92+
Using the custom extension
93+
---------------------------
94+
95+
Using your newly created Twig Extension is no different than any other:
96+
97+
.. code-block:: html+jinja
98+
99+
{# outputs $5,500.00 #}
100+
{{ '5500' | price }}
101+
102+
.. _`Twig official extension repository`: http://github.com/fabpot/Twig-extensions

0 commit comments

Comments
 (0)
0