如何启用aptoma twig-markdown extension?我使用composer
安装了它,但是当我在我的twig文件中使用{% markdown %}
时,收到一条错误消息:
意外的“markdown”标记(期待“block”标记的结束标记 在第8行附近定义。
我查看了symfony文档,但无法找到解决方案。
编辑:
我尝试将以下代码添加到services.yml
,但又出现了另一个错误:
twig.markdown:
class: Aptoma\Twig\Extension\MarkdownExtension
arguments: []
tags:
- { name: twig.extension }
类型错误:参数1传递给 Aptoma \ Twig \ Extension \ MarkdownExtension :: __ construct()必须是 Aptoma \ Twig \ Extension \ MarkdownEngineInterface的实例,没有给出 [...]
根据要求我.twig
- 文件:
{% 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 %}
答案 0 :(得分:1)
根据文档,您需要安装您选择的降价引擎,如:
composer require michelf/php-markdown
您需要为twig扩展和markdown引擎创建服务,将引擎添加到扩展名并将其注册为twig扩展名,例如在services.yml或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会通过使用&#39;标签自动将其注册为枝条扩展名。或标记的服务。
答案 1 :(得分:1)
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\MarkdownEngineInterface
by 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.
But you can use yours. It only have to implement MarkdownEngineInterface
.