From 2bf45c2a3ad493cc664329bc7bd3975b35923a3c Mon Sep 17 00:00:00 2001 From: Matthias Pigulla Date: Mon, 13 Mar 2017 10:09:21 +0100 Subject: [PATCH 1/7] Add instructions on how to use the official Twig extensions repo --- templating/twig_extension.rst | 9 +- .../using_twig_extension_repository.rst | 93 +++++++++++++++++++ 2 files changed, 100 insertions(+), 2 deletions(-) create mode 100644 templating/using_twig_extension_repository.rst diff --git a/templating/twig_extension.rst b/templating/twig_extension.rst index 1b89b0d8e96..188bb7bfe8e 100644 --- a/templating/twig_extension.rst +++ b/templating/twig_extension.rst @@ -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 ` + 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 diff --git a/templating/using_twig_extension_repository.rst b/templating/using_twig_extension_repository.rst new file mode 100644 index 00000000000..d59a6d217cf --- /dev/null +++ b/templating/using_twig_extension_repository.rst @@ -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 + + + + + + + + + + + + + .. 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 `, 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 `. + +.. _`Twig official extension repository`: https://github.com/twigphp/Twig-extensions +.. _`documentation`: http://twig-extensions.readthedocs.io/ From c7b05f7e67ff62b78bf4b452abfdc19b3d8ad579 Mon Sep 17 00:00:00 2001 From: Matthias Pigulla Date: Mon, 13 Mar 2017 10:59:31 +0100 Subject: [PATCH 2/7] Rename article and fix link --- templating/twig_extension.rst | 2 +- ...g_extension_repository.rst => twig_extension_repository.rst} | 0 2 files changed, 1 insertion(+), 1 deletion(-) rename templating/{using_twig_extension_repository.rst => twig_extension_repository.rst} (100%) diff --git a/templating/twig_extension.rst b/templating/twig_extension.rst index 188bb7bfe8e..0d05b05a4dd 100644 --- a/templating/twig_extension.rst +++ b/templating/twig_extension.rst @@ -19,7 +19,7 @@ your code faster. 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 ` + We also have :doc:`a short article ` on how to use extensions from the `Twig official extension repository`_. Create the Extension Class diff --git a/templating/using_twig_extension_repository.rst b/templating/twig_extension_repository.rst similarity index 100% rename from templating/using_twig_extension_repository.rst rename to templating/twig_extension_repository.rst From 120e9be7bdac30a5bb9cf449d24274e7b6595aa6 Mon Sep 17 00:00:00 2001 From: Matthias Pigulla Date: Mon, 13 Mar 2017 13:42:47 +0100 Subject: [PATCH 3/7] Minor tweaks --- templating/twig_extension.rst | 2 ++ templating/twig_extension_repository.rst | 15 +++++++-------- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/templating/twig_extension.rst b/templating/twig_extension.rst index 0d05b05a4dd..ba79f743b8a 100644 --- a/templating/twig_extension.rst +++ b/templating/twig_extension.rst @@ -19,6 +19,8 @@ your code faster. from having a look at the `Twig Bridge`_ which contains most of the extensions provided by the Symfony Framework. +.. tip:: + We also have :doc:`a short article ` on how to use extensions from the `Twig official extension repository`_. diff --git a/templating/twig_extension_repository.rst b/templating/twig_extension_repository.rst index d59a6d217cf..8543ac05aca 100644 --- a/templating/twig_extension_repository.rst +++ b/templating/twig_extension_repository.rst @@ -1,12 +1,12 @@ .. index:: single: Using the Twig Extensions Repository -How to Use the Twig Extension Repository +How to Use the Twig Extensions 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 +useful functions for internationalization, working with arrays and dates. To learn more about these extensions, have a look at their `documentation`_. @@ -40,7 +40,7 @@ Then, define the extension class as a service and tag it with the # app/config/services.yml services: - twig_extension.intl: + twig_extensions.intl: class: Twig_Extensions_Extension_Intl tags: - { name: twig.extension } @@ -55,7 +55,7 @@ Then, define the extension class as a service and tag it with the http://symfony.com/schema/dic/services/services-1.0.xsd"> - @@ -68,7 +68,7 @@ Then, define the extension class as a service and tag it with the use \Twig_Extensions_Extension_Intl; $container - ->register('twig_extension.intl', Twig_Extensions_Extension_Intl::class) + ->register('twig_extensions.intl', Twig_Extensions_Extension_Intl::class) ->addTag('twig.extension'); And that's it! For example, you should now be able to use the @@ -84,10 +84,9 @@ Learning further In the :doc:`reference section `, you can find all the extra Twig functions, filters, tags and tests that are -added by the Symfony Framework. +already 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 `. +We also have documentation on :doc:`how to write your own Twig extension `. .. _`Twig official extension repository`: https://github.com/twigphp/Twig-extensions .. _`documentation`: http://twig-extensions.readthedocs.io/ From 850ad311de4c0f7a7d0cf1127ebf3f9b419758cc Mon Sep 17 00:00:00 2001 From: Matthias Pigulla Date: Mon, 13 Mar 2017 16:33:14 +0100 Subject: [PATCH 4/7] Fix markup/build warning --- templating/twig_extension_repository.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/templating/twig_extension_repository.rst b/templating/twig_extension_repository.rst index 8543ac05aca..8a97f826312 100644 --- a/templating/twig_extension_repository.rst +++ b/templating/twig_extension_repository.rst @@ -2,7 +2,7 @@ single: Using the Twig Extensions Repository How to Use the Twig Extensions Repository -======================================== +========================================= The `Twig official extension repository`_ contains (as of writing) some helpful Twig extensions that are not part of the Twig core. They add From 2beb57bb8d4de64f1db57f71d2668b2f08238f6c Mon Sep 17 00:00:00 2001 From: Matthias Pigulla Date: Mon, 13 Mar 2017 23:34:27 +0100 Subject: [PATCH 5/7] Use a service id that is more unlikely to cause collisions --- templating/twig_extension_repository.rst | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/templating/twig_extension_repository.rst b/templating/twig_extension_repository.rst index 8a97f826312..509be990cac 100644 --- a/templating/twig_extension_repository.rst +++ b/templating/twig_extension_repository.rst @@ -40,7 +40,7 @@ Then, define the extension class as a service and tag it with the # app/config/services.yml services: - twig_extensions.intl: + app.twig_extensions.intl: class: Twig_Extensions_Extension_Intl tags: - { name: twig.extension } @@ -55,7 +55,7 @@ Then, define the extension class as a service and tag it with the http://symfony.com/schema/dic/services/services-1.0.xsd"> - @@ -68,7 +68,7 @@ Then, define the extension class as a service and tag it with the use \Twig_Extensions_Extension_Intl; $container - ->register('twig_extensions.intl', Twig_Extensions_Extension_Intl::class) + ->register('app.twig_extensions.intl', Twig_Extensions_Extension_Intl::class) ->addTag('twig.extension'); And that's it! For example, you should now be able to use the From 52c2a87a67641ae05485dfe3cf31ef7a06c65a70 Mon Sep 17 00:00:00 2001 From: Matthias Pigulla Date: Tue, 14 Mar 2017 10:29:25 +0100 Subject: [PATCH 6/7] Various GH feedback --- templating/twig_extension.rst | 6 +++--- templating/twig_extension_repository.rst | 25 ++++++++++++------------ 2 files changed, 16 insertions(+), 15 deletions(-) diff --git a/templating/twig_extension.rst b/templating/twig_extension.rst index ba79f743b8a..6a3a931a440 100644 --- a/templating/twig_extension.rst +++ b/templating/twig_extension.rst @@ -17,11 +17,11 @@ your code faster. 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. + the extensions provided by the Symfony framework. -.. tip:: +.. seealso:: - We also have :doc:`a short article ` + Read the :doc:`dedicated article ` on how to use extensions from the `Twig official extension repository`_. Create the Extension Class diff --git a/templating/twig_extension_repository.rst b/templating/twig_extension_repository.rst index 509be990cac..86cf3756efd 100644 --- a/templating/twig_extension_repository.rst +++ b/templating/twig_extension_repository.rst @@ -4,17 +4,17 @@ How to Use the Twig Extensions Repository ========================================= -The `Twig official extension repository`_ contains (as of writing) some +The `Twig official extensions repository`_ contains 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). +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. +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. @@ -24,8 +24,7 @@ that repository so you can use it in your Twig templates. 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 +First, install the Twig extensions using Composer. .. code-block:: terminal @@ -42,6 +41,7 @@ Then, define the extension class as a service and tag it with the services: app.twig_extensions.intl: class: Twig_Extensions_Extension_Intl + public: false tags: - { name: twig.extension } @@ -55,7 +55,7 @@ Then, define the extension class as a service and tag it with the http://symfony.com/schema/dic/services/services-1.0.xsd"> - @@ -65,15 +65,15 @@ Then, define the extension class as a service and tag it with the .. code-block:: php // app/config/services.php - use \Twig_Extensions_Extension_Intl; $container - ->register('app.twig_extensions.intl', Twig_Extensions_Extension_Intl::class) + ->register('app.twig_extensions.intl', \Twig_Extensions_Extension_Intl::class) + ->setPublic(false) ->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: +``localizeddate`` filter to format a date according to the +current request's locale: .. code-block:: twig @@ -86,7 +86,8 @@ In the :doc:`reference section `, you can find all the extra Twig functions, filters, tags and tests that are already added by the Symfony Framework. -We also have documentation on :doc:`how to write your own Twig extension `. +Also, documentation is available on :doc:`how to write your own Twig +extension `. .. _`Twig official extension repository`: https://github.com/twigphp/Twig-extensions .. _`documentation`: http://twig-extensions.readthedocs.io/ From 5a4321a0844ecf30a6054eb725ca06fa2306a93e Mon Sep 17 00:00:00 2001 From: Matthias Pigulla Date: Sun, 19 Mar 2017 21:31:53 +0100 Subject: [PATCH 7/7] Fix link --- templating/twig_extension_repository.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/templating/twig_extension_repository.rst b/templating/twig_extension_repository.rst index 86cf3756efd..d83e83ee539 100644 --- a/templating/twig_extension_repository.rst +++ b/templating/twig_extension_repository.rst @@ -89,5 +89,5 @@ already added by the Symfony Framework. Also, documentation is available on :doc:`how to write your own Twig extension `. -.. _`Twig official extension repository`: https://github.com/twigphp/Twig-extensions +.. _`Twig official extensions repository`: https://github.com/twigphp/Twig-extensions .. _`documentation`: http://twig-extensions.readthedocs.io/