Skip to content

Update best_practices/templates.rst #7902

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

Merged
merged 1 commit into from
May 16, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 10 additions & 27 deletions best_practices/templates.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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;
Expand All @@ -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

Expand Down Expand Up @@ -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 <services-autoconfigure>` your twig
extension and inject a ``Markdown`` instance in it thanks to
:doc:`autowiring </service_container/autowiring>`.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After #7906, I have something we can link to that talks about this "default" config. I'm linking to it everywhere pretty consistently. See https://github.com/symfony/symfony-docs/pull/7906/files#diff-37bf298125afc3dde92bb3907795a9e7R291 for an example

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added it in sha: 2a7f91d :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like it, thanks 👍


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