Skip to content

Commit 2bf45c2

Browse files
committed
Add instructions on how to use the official Twig extensions repo
1 parent 4e7ae43 commit 2bf45c2

File tree

2 files changed

+100
-2
lines changed

2 files changed

+100
-2
lines changed

templating/twig_extension.rst

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,12 @@ your code faster.
1515

1616
.. tip::
1717

18-
Before writing your own extensions, have a look at the
19-
`Twig official extension repository`_.
18+
When writing your own extensions, you might want to learn
19+
from having a look at the `Twig Bridge`_ which contains most of
20+
the extensions provided by the Symfony Framework.
21+
22+
We also have :doc:`a short article </reference/using_twig_extension_repository>`
23+
on how to use extensions from the `Twig official extension repository`_.
2024

2125
Create the Extension Class
2226
--------------------------
@@ -131,3 +135,4 @@ For a more in-depth look into Twig Extensions, please take a look at the
131135
.. _`global variables`: http://twig.sensiolabs.org/doc/advanced.html#id1
132136
.. _`functions`: http://twig.sensiolabs.org/doc/advanced.html#id2
133137
.. _`Twig extensions documentation legacy`: http://twig.sensiolabs.org/doc/advanced_legacy.html#creating-an-extension
138+
.. _`Twig Bridge`: https://github.com/symfony/symfony/tree/master/src/Symfony/Bridge/Twig/Extension
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
.. index::
2+
single: Using the Twig Extensions Repository
3+
4+
How to Use the Twig Extension Repository
5+
========================================
6+
7+
The `Twig official extension repository`_ contains (as of writing) some
8+
helpful Twig extensions that are not part of the Twig core. They add
9+
useful functions for internationalization, working with Arrays and
10+
dates. To learn more about these extensions, have a look at their
11+
`documentation`_.
12+
13+
This repository is meant as an extension to Twig in general. So, it
14+
is does *not* provide a direct means to register itself with the
15+
Symfony Framework (it is not a Bundle).
16+
17+
It is, however, very easy to get the extensions set-up in Symfony.
18+
This article will show you how to register the ``Intl`` extension from
19+
that repository so you can use it in your Twig templates.
20+
21+
.. tip::
22+
23+
Setting up one of the other extensions works just the same way,
24+
except you need to choose another service id and have to use
25+
the right class name.
26+
27+
First, add the Twig Extensions repository as a dependency in your
28+
project. Assuming you are using Composer, run
29+
30+
.. code-block:: terminal
31+
32+
$ composer require twig/extensions
33+
34+
Then, define the extension class as a service and tag it with the
35+
``twig.extension`` tag.
36+
37+
.. configuration-block::
38+
39+
.. code-block:: yaml
40+
41+
# app/config/services.yml
42+
services:
43+
twig_extension.intl:
44+
class: Twig_Extensions_Extension_Intl
45+
tags:
46+
- { name: twig.extension }
47+
48+
.. code-block:: xml
49+
50+
<!-- app/config/services.xml -->
51+
<?xml version="1.0" encoding="UTF-8" ?>
52+
<container xmlns="http://symfony.com/schema/dic/services"
53+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
54+
xsi:schemaLocation="http://symfony.com/schema/dic/services
55+
http://symfony.com/schema/dic/services/services-1.0.xsd">
56+
57+
<services>
58+
<service id="twig_extension.intl"
59+
class="Twig_Extensions_Extension_Intl">
60+
<tag name="twig.extension" />
61+
</service>
62+
</services>
63+
</container>
64+
65+
.. code-block:: php
66+
67+
// app/config/services.php
68+
use \Twig_Extensions_Extension_Intl;
69+
70+
$container
71+
->register('twig_extension.intl', Twig_Extensions_Extension_Intl::class)
72+
->addTag('twig.extension');
73+
74+
And that's it! For example, you should now be able to use the
75+
``localizeddate`` filter to format a date according to the request's
76+
current locale:
77+
78+
.. code-block:: twig
79+
80+
{{ post.published_at|localizeddate('medium', 'none', locale) }}
81+
82+
Learning further
83+
----------------
84+
85+
In the :doc:`reference section </reference/twig_reference>`, you can
86+
find all the extra Twig functions, filters, tags and tests that are
87+
added by the Symfony Framework.
88+
89+
When that does not meet your particular needs, we also have
90+
documentation on :doc:`how to write your own Twig extension </templating/twig_extension>`.
91+
92+
.. _`Twig official extension repository`: https://github.com/twigphp/Twig-extensions
93+
.. _`documentation`: http://twig-extensions.readthedocs.io/

0 commit comments

Comments
 (0)