Symfony3: Enable aptoma twig-markdown extension

581 views Asked by At

How can I enable the aptoma twig-markdown extension? I have installed it using composer but when I use {% markdown %} in my twig file, I get an error message saying:

Unexpected "markdown" tag (expecting closing tag for the "block" tag defined near line 8).

I had a look in the symfony documentation but was not able to find the solution.

Edit:

I tried to add the following code to services.yml but got another error:

twig.markdown:
    class: Aptoma\Twig\Extension\MarkdownExtension
    arguments: []
    tags:
        - { name: twig.extension }

Type error: Argument 1 passed to Aptoma\Twig\Extension\MarkdownExtension::__construct() must be an instance of Aptoma\Twig\Extension\MarkdownEngineInterface, none given [...]

Upon request my .twig-file:

{% extends 'XYZBundle::layout.html.twig' %}

{% block title %}
    {{ parent() }} – Eintrag anzeigen
{% endblock %}

{% block platform_body %}
    <ul>
        <li>
            <a href="{{ path('work_index') }}">Back to the list</a>
        </li>
        <li>
            <a href="{{ path('work_edit', { 'id': work.id }) }}">Edit</a>
        </li>
    </ul>
    <h1>{{ work.title }}</h1>

    <div class="work-content">
    {% markdown %}
    {{ work.content }}
    {% endmarkdown %}
    </div>
{% endblock %}
2

There are 2 answers

5
ivoba On

According to the docs you need to install the markdown engine of your choice like:

composer require michelf/php-markdown

You need to create a service for the twig extension and the markdown engine, add the engine to the extension and register it as twig extension, f.e like this in your services.yml or config.yml:

services:
    markdown.engine:
        class: Aptoma\Twig\Extension\MarkdownEngine\MichelfMarkdownEngine
    twig.markdown:
        class: Aptoma\Twig\Extension\MarkdownExtension
        arguments: ['@markdown.engine']
        tags:
            - { name: twig.extension }

Symfony will then automatically register it as twig extension through the usage of 'tags' or tagged services.

0
Alexandre Tranchant On

According to the new autowire system, you only have to declare the mardown engine implementing the Aptoma markdown engine interface.

Aptoma\Twig\Extension\MarkdownEngineInterface:
    class: the markdown engine of your choice!

twig.extension.markdown:
    class: Aptoma\Twig\Extension\MarkdownExtension
    tags:
        - { name: twig.extension }

If you do not change Aptoma\Twig\Extension\MarkdownEngineInterfaceby something like markdown.engine you do not need to declare arguments in your twig extension service declaration.

If you want to use the recommended markdown engine, install it via:

composer require michelf/php-markdown

Then, declare it:

Aptoma\Twig\Extension\MarkdownEngineInterface:
    class: Aptoma\Twig\Extension\MarkdownEngine\MichelfMarkdownEngine 

twig.extension.markdown:
    class: Aptoma\Twig\Extension\MarkdownExtension
    tags:
        - { name: twig.extension }

You can see in vendor\aptoma subdirectories the available engine. The available engine

But you can use yours. It only have to implement MarkdownEngineInterface.