PHPackages                             mccool/twig-markdown - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Templating &amp; Views](/categories/templating)
4. /
5. mccool/twig-markdown

ActiveLibrary[Templating &amp; Views](/categories/templating)

mccool/twig-markdown
====================

Twig extension to work with Markdown content

4.0.2(5y ago)037MITPHPPHP &gt;=7.0

Since Nov 18Pushed 5y ago1 watchersCompare

[ Source](https://github.com/ShawnMcCool/twig-markdown)[ Packagist](https://packagist.org/packages/mccool/twig-markdown)[ RSS](/packages/mccool-twig-markdown/feed)WikiDiscussions master Synced 2mo ago

READMEChangelogDependencies (7)Versions (13)Used By (0)

Twig Markdown Extension
=======================

[](#twig-markdown-extension)

[![Build Status](https://camo.githubusercontent.com/bf3e5f467c541682b4a16f971cffcc1e175bc48b9d241e9db3a540d286782364/68747470733a2f2f7365637572652e7472617669732d63692e6f72672f6170746f6d612f747769672d6d61726b646f776e2e706e673f6272616e63683d6d6173746572)](http://travis-ci.org/aptoma/twig-markdown)

Twig Markdown extension provides a new filter and a tag to allow parsing of content as Markdown in [Twig](http://twig.sensiolabs.org) templates.

This extension could be integrated with several Markdown parser as it provides an interface, which allows you to customize your Markdown parser.

### Supported parsers

[](#supported-parsers)

- [michelf/php-markdown](https://github.com/michelf/php-markdown) (+ MarkdownExtra)
- [league/commonmark](http://commonmark.thephpleague.com/)
- [KnpLabs/php-github-api](https://github.com/KnpLabs/php-github-api)
- [erusev/parsedown](https://github.com/erusev/parsedown)

Features
--------

[](#features)

- Filter support `{{ "# Heading Level 1"|markdown }}`
- Tag support `{% markdown %}{% endmarkdown %}`

When used as a tag, the indentation level of the first line sets the default indentation level for the rest of the tag content. From this indentation level, all same indentation or outdented levels text will be transformed as regular text.

This feature allows you to write your Markdown content at any indentation level without caring of Markdown internal transformation:

```

    {{ title }}

    {% markdown %}
    This is a list that is indented to match the context around the markdown tag:

    * List item 1
    * List item 2
        * Sub List Item
            * Sub Sub List Item

    The following block will be transformed as code, as it is indented more than the
    surrounding content:

        $code = "good";

    {% endmarkdown %}

```

Installation
------------

[](#installation)

Run the composer command to install the latest stable version:

```
composer require aptoma/twig-markdown
```

Or update your `composer.json`:

```
{
    "require": {
        "aptoma/twig-markdown": "~2.0"
    }
}
```

You can choose to provide your own Markdown engine, although we recommend using [michelf/php-markdown](https://github.com/michelf/php-markdown):

```
composer require michelf/php-markdown ~1.3
```

```
{
    "require": {
        "michelf/php-markdown": "~1.3"
    }
}
```

You may also use the [PHP League CommonMark engine](http://commonmark.thephpleague.com/):

```
composer require league/commonmark ~0.5
```

```
{
    "require": {
        "league/commonmark": "~0.5"
    }
}
```

Usage
-----

[](#usage)

### Twig Extension

[](#twig-extension)

The Twig extension provides the `markdown` tag and filter support.

Assuming that you are using [composer](http://getcomposer.org) autoloading, add the extension to the Twig environment:

```
use Aptoma\Twig\Extension\MarkdownExtension;
use Aptoma\Twig\Extension\MarkdownEngine;

$engine = new MarkdownEngine\MichelfMarkdownEngine();

$twig->addExtension(new MarkdownExtension($engine));
```

### Twig Token Parser

[](#twig-token-parser)

The Twig token parser provides the `markdown` tag only!

```
use Aptoma\Twig\Extension\MarkdownEngine;
use Aptoma\Twig\TokenParser\MarkdownTokenParser;

$engine = new MarkdownEngine\MichelfMarkdownEngine();

$twig->addTokenParser(new MarkdownTokenParser($engine));
```

### GitHub Markdown Engine

[](#github-markdown-engine)

`MarkdownEngine\GitHubMarkdownEngine` provides an interface to GitHub's markdown engine using their public API via [`KnpLabs\php-github-api`](https://github.com/knplabs/php-github-api). To reduce API calls, rendered documents are hashed and cached in the filesystem. You can pass a GitHub repository and the path to be used for caching to the constructor:

```
use Aptoma\Twig\Extension\MarkdownEngine;

$engine = new MarkdownEngine\GitHubMarkdownEngine(
    'aptoma/twig-markdown', // The GitHub repository to use as a context
    true,                   // Whether to use GitHub's Flavored Markdown (GFM)
    '/tmp/markdown-cache',  // Path on filesystem to store rendered documents
);
```

In order to authenticate the API client (for instance), it's possible to pass an own instance of `\GitHub\Client` instead of letting the engine create one itself:

```
$client = new \Github\Client;
$client->authenticate('GITHUB_CLIENT_ID', 'GITHUB_CLIENT_SECRET', \Github\Client::AUTH_URL_CLIENT_ID);

$engine = new MarkdownEngine\GitHubMarkdownEngine('aptoma/twig-markdown', true, '/tmp/markdown-cache', $client);
```

### Using a different Markdown parser engine

[](#using-a-different-markdown-parser-engine)

If you want to use a different Markdown parser, you need to create an adapter that implements `Aptoma\Twig\Extension\MarkdownEngineInterface.php`. Have a look at `Aptoma\Twig\Extension\MarkdownEngine\MichelfMarkdownEngine` for an example.

Tests
-----

[](#tests)

The test suite uses PHPUnit:

```
$ phpunit

```

License
-------

[](#license)

Twig Markdown Extension is licensed under the MIT license.

###  Health Score

29

—

LowBetter than 60% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity7

Limited adoption so far

Community15

Small or concentrated contributor base

Maturity65

Established project with proven stability

 Bus Factor1

Top contributor holds 72% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~293 days

Recently: every ~465 days

Total

11

Last Release

1988d ago

Major Versions

0.2.1 → 1.0.02014-07-24

1.2.0 → 2.0.02015-10-23

2.0.0 → 3.0.02017-06-23

3.0.0 → 4.0.02019-03-06

PHP version history (2 changes)3.0.0PHP ^7.0

4.0.2PHP &gt;=7.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/6da00bb361f9beaa0dbd9577dd2b3c35089dd5d6ae9a3b57003d09f7737739b0?d=identicon)[ShawnMcCool](/maintainers/ShawnMcCool)

---

Top Contributors

[![gunnarlium](https://avatars.githubusercontent.com/u/207556?v=4)](https://github.com/gunnarlium "gunnarlium (36 commits)")[![stof](https://avatars.githubusercontent.com/u/439401?v=4)](https://github.com/stof "stof (4 commits)")[![lukas-w](https://avatars.githubusercontent.com/u/2879917?v=4)](https://github.com/lukas-w "lukas-w (3 commits)")[![SebLours](https://avatars.githubusercontent.com/u/5138394?v=4)](https://github.com/SebLours "SebLours (2 commits)")[![qasim](https://avatars.githubusercontent.com/u/3253505?v=4)](https://github.com/qasim "qasim (1 commits)")[![JeroenDeDauw](https://avatars.githubusercontent.com/u/146040?v=4)](https://github.com/JeroenDeDauw "JeroenDeDauw (1 commits)")[![eexit](https://avatars.githubusercontent.com/u/57098?v=4)](https://github.com/eexit "eexit (1 commits)")[![jakeasmith](https://avatars.githubusercontent.com/u/234832?v=4)](https://github.com/jakeasmith "jakeasmith (1 commits)")[![caseyamcl](https://avatars.githubusercontent.com/u/53035?v=4)](https://github.com/caseyamcl "caseyamcl (1 commits)")

---

Tags

twigmarkdown

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/mccool-twig-markdown/health.svg)

```
[![Health](https://phpackages.com/badges/mccool-twig-markdown/health.svg)](https://phpackages.com/packages/mccool-twig-markdown)
```

###  Alternatives

[twig/extra-bundle

A Symfony bundle for extra Twig extensions

91292.0M313](/packages/twig-extra-bundle)[twig/markdown-extra

A Twig extension for Markdown

12114.3M83](/packages/twig-markdown-extra)[twig/intl-extra

A Twig extension for Intl

36663.2M218](/packages/twig-intl-extra)[picocms/pico

Pico is a flat file CMS, this means there is no administration backend and database to deal with. You simply create .md files in the "content" folder and that becomes a page.

3.9k37.8k11](/packages/picocms-pico)[twig/string-extra

A Twig extension for Symfony String

21946.0M132](/packages/twig-string-extra)[twig/cssinliner-extra

A Twig extension to allow inlining CSS

23018.5M53](/packages/twig-cssinliner-extra)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
