|
| 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