From 8769a30419e67f7f37cd161d2ecddf47da56c291 Mon Sep 17 00:00:00 2001 From: Guilhem Niot Date: Fri, 12 May 2017 18:53:47 +0200 Subject: [PATCH] Update best_practices/templates.rst --- best_practices/templates.rst | 37 ++++++++++-------------------------- 1 file changed, 10 insertions(+), 27 deletions(-) diff --git a/best_practices/templates.rst b/best_practices/templates.rst index 9ec92925afe..7e562f26908 100644 --- a/best_practices/templates.rst +++ b/best_practices/templates.rst @@ -60,8 +60,8 @@ Twig Extensions .. best-practice:: - Define your Twig extensions in the ``AppBundle/Twig/`` directory and - configure them using the ``app/config/services.yml`` file. + Define your Twig extensions in the ``AppBundle/Twig/`` directory. Your + application will automatically detect them and configure them. Our application needs a custom ``md2html`` Twig filter so that we can transform the Markdown contents of each post into HTML. @@ -73,18 +73,8 @@ a new dependency of the project: $ composer require erusev/parsedown -Then, create a new ``Markdown`` service that will be used later by the Twig -extension. The service definition only requires the path to the class: - -.. code-block:: yaml - - # app/config/services.yml - services: - # ... - app.markdown: - class: AppBundle\Utils\Markdown - -And the ``Markdown`` class just needs to define one single method to transform +Then, create a new ``Markdown`` class that will be used later by the Twig +extension. It just needs to define one single method to transform Markdown content into HTML:: namespace AppBundle\Utils; @@ -107,8 +97,8 @@ Markdown content into HTML:: } Next, create a new Twig extension and define a new filter called ``md2html`` -using the ``Twig_SimpleFilter`` class. Inject the newly defined ``markdown`` -service in the constructor of the Twig extension: +using the ``Twig_SimpleFilter`` class. Inject the newly defined ``Markdown`` +class in the constructor of the Twig extension: .. code-block:: php @@ -147,18 +137,11 @@ service in the constructor of the Twig extension: } } -Lastly define a new service to enable this Twig extension in the app (the service -name is irrelevant because you never use it in your own code): - -.. code-block:: yaml +And that's it! - # app/config/services.yml - services: - app.twig.app_extension: - class: AppBundle\Twig\AppExtension - arguments: ['@app.markdown'] - public: false - tags: [twig.extension] +Your application will :ref:`autoconfigure ` your twig +extension and inject a ``Markdown`` instance in it thanks to +:doc:`autowiring `. .. _`Twig`: http://twig.sensiolabs.org/ .. _`Parsedown`: http://parsedown.org/