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