twigphp/Twig · error · LogicException

You cannot use the "markdown_to_html" filter as no Markdown…

Error message

You cannot use the "markdown_to_html" filter as no Markdown library is available; try running "composer require league/commonmark".

What it means

DefaultMarkdown's constructor probes for any supported Markdown library (league/commonmark, michelf/php-markdown, erusev/parsedown, tempest/markdown) via class_exists(). If none is installed it throws a LogicException, because the markdown_to_html filter has no converter to delegate to. Being a LogicException, it typically surfaces during container wiring/filter setup rather than per-render.

Solutions

  1. Run "composer require league/commonmark" (the recommended library).
  2. Or install another supported lib: michelf/php-markdown, erusev/parsedown, or tempest/markdown.
  3. Or implement MarkdownInterface yourself and inject it instead of DefaultMarkdown.

Example fix

// before
composer require twig/markdown-extra
// after
composer require twig/markdown-extra league/commonmark
Defensive patterns

Strategy: fallback

Validate before calling

if (!class_exists(\League\CommonMark\CommonMarkConverter::class)
 && !class_exists(\Michelf\MarkdownExtra::class)
 && !class_exists(\Parsedown::class)
 && !class_exists(\Tempest\Markdown\Markdown::class)) {
    throw new LogicException('markdown_to_html requires a Markdown package; composer require league/commonmark');
}

Try / catch

try {
    $markdown = new \Twig\Extra\Markdown\DefaultMarkdown();
} catch (\LogicException $e) {
    // install a Markdown package or provide a custom MarkdownInterface implementation
}

Prevention

When it happens

Trigger: Installing twig/markdown-extra (or registering MarkdownExtension) without installing any of the Markdown packages it supports, then constructing DefaultMarkdown or using the markdown_to_html filter.

Common situations: composer require twig/markdown-extra alone (it does not pull a Markdown lib by itself); moving to a new server/CI image without composer install of optional deps; removing league/commonmark during dependency cleanup while templates still use the filter.

Understand the failure class

Background: "X is not installed. Please install it with pip install Y": missing optional dependency errors — ImportError/ValueError raised when a library's optional extra was never installed — this error's family across 22 libraries.

Related errors


AI-assisted analysis of twigphp/Twig@a414c3a491 (2026-09-13). Data as JSON: /api/errors/e3da05b1d4ff3cc6. Report an issue: GitHub.

Appendix: source

Thrown at extra/markdown-extra/DefaultMarkdown.php:33

use Michelf\MarkdownExtra;
use Tempest\Markdown\Markdown;

class DefaultMarkdown implements MarkdownInterface
{
    private $converter;

    public function __construct()
    {
        if (class_exists(CommonMarkConverter::class)) {
            $this->converter = new LeagueMarkdown();
        } elseif (class_exists(MarkdownExtra::class)) {
            $this->converter = new MichelfMarkdown();
        } elseif (class_exists(\Parsedown::class)) {
            $this->converter = new ErusevMarkdown();
        } elseif (class_exists(Markdown::class)) {
            $this->converter = new TempestMarkdown();
        } else {
            throw new \LogicException('You cannot use the "markdown_to_html" filter as no Markdown library is available; try running "composer require league/commonmark".');
        }
    }

    public function convert(string $body): string
    {
        return $this->converter->convert($body);
    }
}

View on GitHub (pinned to a414c3a491)