-
-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Add instructions on how to use the official Twig extensions repo #7616
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
2bf45c2
c7b05f7
120e9be
850ad31
2beb57b
52c2a87
5a4321a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,8 +15,12 @@ your code faster. | |
|
||
.. tip:: | ||
|
||
Before writing your own extensions, have a look at the | ||
`Twig official extension repository`_. | ||
When writing your own extensions, you might want to learn | ||
from having a look at the `Twig Bridge`_ which contains most of | ||
the extensions provided by the Symfony Framework. | ||
|
||
We also have :doc:`a short article </reference/using_twig_extension_repository>` | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. maybe we should also rename the new file to something shorter like There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed |
||
on how to use extensions from the `Twig official extension repository`_. | ||
|
||
Create the Extension Class | ||
-------------------------- | ||
|
@@ -131,3 +135,4 @@ For a more in-depth look into Twig Extensions, please take a look at the | |
.. _`global variables`: http://twig.sensiolabs.org/doc/advanced.html#id1 | ||
.. _`functions`: http://twig.sensiolabs.org/doc/advanced.html#id2 | ||
.. _`Twig extensions documentation legacy`: http://twig.sensiolabs.org/doc/advanced_legacy.html#creating-an-extension | ||
.. _`Twig Bridge`: https://github.com/symfony/symfony/tree/master/src/Symfony/Bridge/Twig/Extension |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
.. index:: | ||
single: Using the Twig Extensions Repository | ||
|
||
How to Use the Twig Extension Repository | ||
======================================== | ||
|
||
The `Twig official extension repository`_ contains (as of writing) some | ||
helpful Twig extensions that are not part of the Twig core. They add | ||
useful functions for internationalization, working with Arrays and | ||
dates. To learn more about these extensions, have a look at their | ||
`documentation`_. | ||
|
||
This repository is meant as an extension to Twig in general. So, it | ||
is does *not* provide a direct means to register itself with the | ||
Symfony Framework (it is not a Bundle). | ||
|
||
It is, however, very easy to get the extensions set-up in Symfony. | ||
This article will show you how to register the ``Intl`` extension from | ||
that repository so you can use it in your Twig templates. | ||
|
||
.. tip:: | ||
|
||
Setting up one of the other extensions works just the same way, | ||
except you need to choose another service id and have to use | ||
the right class name. | ||
|
||
First, add the Twig Extensions repository as a dependency in your | ||
project. Assuming you are using Composer, run | ||
|
||
.. code-block:: terminal | ||
|
||
$ composer require twig/extensions | ||
|
||
Then, define the extension class as a service and tag it with the | ||
``twig.extension`` tag. | ||
|
||
.. configuration-block:: | ||
|
||
.. code-block:: yaml | ||
|
||
# app/config/services.yml | ||
services: | ||
twig_extension.intl: | ||
class: Twig_Extensions_Extension_Intl | ||
tags: | ||
- { name: twig.extension } | ||
|
||
.. code-block:: xml | ||
|
||
<!-- app/config/services.xml --> | ||
<?xml version="1.0" encoding="UTF-8" ?> | ||
<container xmlns="http://symfony.com/schema/dic/services" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://symfony.com/schema/dic/services | ||
http://symfony.com/schema/dic/services/services-1.0.xsd"> | ||
|
||
<services> | ||
<service id="twig_extension.intl" | ||
class="Twig_Extensions_Extension_Intl"> | ||
<tag name="twig.extension" /> | ||
</service> | ||
</services> | ||
</container> | ||
|
||
.. code-block:: php | ||
|
||
// app/config/services.php | ||
use \Twig_Extensions_Extension_Intl; | ||
|
||
$container | ||
->register('twig_extension.intl', Twig_Extensions_Extension_Intl::class) | ||
->addTag('twig.extension'); | ||
|
||
And that's it! For example, you should now be able to use the | ||
``localizeddate`` filter to format a date according to the request's | ||
current locale: | ||
|
||
.. code-block:: twig | ||
|
||
{{ post.published_at|localizeddate('medium', 'none', locale) }} | ||
|
||
Learning further | ||
---------------- | ||
|
||
In the :doc:`reference section </reference/twig_reference>`, you can | ||
find all the extra Twig functions, filters, tags and tests that are | ||
added by the Symfony Framework. | ||
|
||
When that does not meet your particular needs, we also have | ||
documentation on :doc:`how to write your own Twig extension </templating/twig_extension>`. | ||
|
||
.. _`Twig official extension repository`: https://github.com/twigphp/Twig-extensions | ||
.. _`documentation`: http://twig-extensions.readthedocs.io/ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
framework