Skip to content

Commit d30928d

Browse files
committed
minor #7902 Update best_practices/templates.rst (GuilhemN)
This PR was merged into the master branch. Discussion ---------- Update best_practices/templates.rst First example of a doc asserting we're using the 3.3 SE (#7877 (comment)). Commits ------- 8769a30 Update best_practices/templates.rst
2 parents 14cd4c4 + 8769a30 commit d30928d

File tree

1 file changed

+10
-27
lines changed

1 file changed

+10
-27
lines changed

best_practices/templates.rst

Lines changed: 10 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,8 @@ Twig Extensions
6060

6161
.. best-practice::
6262

63-
Define your Twig extensions in the ``AppBundle/Twig/`` directory and
64-
configure them using the ``app/config/services.yml`` file.
63+
Define your Twig extensions in the ``AppBundle/Twig/`` directory. Your
64+
application will automatically detect them and configure them.
6565

6666
Our application needs a custom ``md2html`` Twig filter so that we can transform
6767
the Markdown contents of each post into HTML.
@@ -73,18 +73,8 @@ a new dependency of the project:
7373
7474
$ composer require erusev/parsedown
7575
76-
Then, create a new ``Markdown`` service that will be used later by the Twig
77-
extension. The service definition only requires the path to the class:
78-
79-
.. code-block:: yaml
80-
81-
# app/config/services.yml
82-
services:
83-
# ...
84-
app.markdown:
85-
class: AppBundle\Utils\Markdown
86-
87-
And the ``Markdown`` class just needs to define one single method to transform
76+
Then, create a new ``Markdown`` class that will be used later by the Twig
77+
extension. It just needs to define one single method to transform
8878
Markdown content into HTML::
8979

9080
namespace AppBundle\Utils;
@@ -107,8 +97,8 @@ Markdown content into HTML::
10797
}
10898

10999
Next, create a new Twig extension and define a new filter called ``md2html``
110-
using the ``Twig_SimpleFilter`` class. Inject the newly defined ``markdown``
111-
service in the constructor of the Twig extension:
100+
using the ``Twig_SimpleFilter`` class. Inject the newly defined ``Markdown``
101+
class in the constructor of the Twig extension:
112102

113103
.. code-block:: php
114104
@@ -147,18 +137,11 @@ service in the constructor of the Twig extension:
147137
}
148138
}
149139
150-
Lastly define a new service to enable this Twig extension in the app (the service
151-
name is irrelevant because you never use it in your own code):
152-
153-
.. code-block:: yaml
140+
And that's it!
154141

155-
# app/config/services.yml
156-
services:
157-
app.twig.app_extension:
158-
class: AppBundle\Twig\AppExtension
159-
arguments: ['@app.markdown']
160-
public: false
161-
tags: [twig.extension]
142+
Your application will :ref:`autoconfigure <services-autoconfigure>` your twig
143+
extension and inject a ``Markdown`` instance in it thanks to
144+
:doc:`autowiring </service_container/autowiring>`.
162145

163146
.. _`Twig`: http://twig.sensiolabs.org/
164147
.. _`Parsedown`: http://parsedown.org/

0 commit comments

Comments
 (0)