我想使用markdown_to_html过滤器在我的模板中呈现缩小,所以我尝试跟随文档中的使用说明,但是过滤器没有被正确地添加。呈现模板时遇到的错误是消息:未知的"markdown_to_html“过滤器。
这是我的密码:
// Load twig and the markdown extension
require_once "../vendor/autoload.php";
$loader = new \Twig\Loader\FilesystemLoader("../templates");
$twig = new \Twig\Environment($loader, [
"cache" => "../cache/twig",
"debug" => true,
"autoescape" => false,
]);
use Twig\Extra\Markdown\DefaultMarkdown;
use Twig\Extra\Markdown\MarkdownRuntime;
use Twig\RuntimeLoader\RuntimeLoaderInterface;
$twig->addRuntimeLoader(new class implements RuntimeLoaderInterface {
public function load($class) {
if (MarkdownRuntime::class === $class) {
return new MarkdownRuntime(new DefaultMarkdown());
}
}
});
// Read md file and render template
$markdown = file_get_contents("content/text.md");
try {
echo $twig->render("template.phtml", ["text" => $markdown]);
}
catch(Exception $e) {
echo 'Message: ' . $e->getMessage();
}模板:
{% apply markdown_to_html %}
{{ text }}
{% endapply %}我添加了twig/twig和twig/markdown-extra,它们都是3.0版本。谢谢你的帮助。
发布于 2020-05-06 12:52:01
您忘记将扩展添加到
$twig->addExtension(new MarkdownExtension());注意文档是怎么说的
如果不使用Symfony,则必须同时使用注册扩展运行时:
你为什么要这么做而不是
https://stackoverflow.com/questions/61634198
复制相似问题