1``markdown_to_html``
2====================
3
4.. versionadded:: 2.12
5
6    The ``markdown_to_html`` filter was added in Twig 2.12.
7
8The ``markdown_to_html`` filter converts a block of Markdown to HTML:
9
10.. code-block:: twig
11
12    {% apply markdown_to_html %}
13    Title
14    ======
15
16    Hello!
17    {% endapply %}
18
19Note that you can indent the Markdown content as leading whitespaces will be
20removed consistently before conversion:
21
22.. code-block:: twig
23
24    {% apply markdown_to_html %}
25        Title
26        ======
27
28        Hello!
29    {% endapply %}
30
31You can also use the filter on an included file or a variable:
32
33.. code-block:: twig
34
35    {{ include('some_template.markdown.twig')|markdown_to_html }}
36
37    {{ changelog|markdown_to_html }}
38
39.. note::
40
41    The ``markdown_to_html`` filter is part of the ``MarkdownExtension`` which
42    is not installed by default. Install it first:
43
44    .. code-block:: bash
45
46        $ composer require twig/markdown-extra
47
48    Then, on Symfony projects, install the ``twig/extra-bundle``:
49
50    .. code-block:: bash
51
52        $ composer require twig/extra-bundle
53
54    Otherwise, add the extension explicitly on the Twig environment::
55
56        use Twig\Extra\Markdown\MarkdownExtension;
57
58        $twig = new \Twig\Environment(...);
59        $twig->addExtension(new MarkdownExtension());
60
61    If you are not using Symfony, you must also register the extension runtime::
62
63        use Twig\Extra\Markdown\DefaultMarkdown;
64        use Twig\Extra\Markdown\MarkdownRuntime;
65        use Twig\RuntimeLoader\RuntimeLoaderInterface;
66
67        $twig->addRuntimeLoader(new class implements RuntimeLoaderInterface {
68            public function load($class) {
69                if (MarkdownRuntime::class === $class) {
70                    return new MarkdownRuntime(new DefaultMarkdown());
71                }
72            }
73        });
74
75    Afterwards you need to install a markdown library of your choice. Some of them are
76    mentioned in the ``require-dev`` section of the ``twig/markdown-extra`` package.
77