PHPackages                             ramsey/twig-codeblock - 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. ramsey/twig-codeblock

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

ramsey/twig-codeblock
=====================

A Twig extension for defining blocks of code for syntax highlighting (with Pygments) and more.

3.0.0(1y ago)247223[2 PRs](https://github.com/ramsey/twig-codeblock/pulls)2MITPHPPHP ^8.2CI passing

Since Feb 27Pushed 9mo ago1 watchersCompare

[ Source](https://github.com/ramsey/twig-codeblock)[ Packagist](https://packagist.org/packages/ramsey/twig-codeblock)[ RSS](/packages/ramsey-twig-codeblock/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (5)Dependencies (16)Versions (7)Used By (2)

ramsey/twig-codeblock
=====================

[](#ramseytwig-codeblock)

 **🌿 Syntax highlighting for Twig with the `{% codeblock %}` tag.**

 [![Source Code](https://camo.githubusercontent.com/ea6d375a662cc03ab6346b5c9c8bac8c58017e1512c5e683b85b069da072a3fd/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f736f757263652d72616d7365792f747769672d2d636f6465626c6f636b2d626c75652e7376673f7374796c653d666c61742d737175617265)](https://github.com/ramsey/twig-codeblock) [![Download Package](https://camo.githubusercontent.com/535a0bbe51a4616fbb313dc757c807a23ab6079ff8d2251414af02600eccb0b8/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f72616d7365792f747769672d636f6465626c6f636b2e7376673f7374796c653d666c61742d737175617265266c6162656c3d72656c65617365)](https://packagist.org/packages/ramsey/twig-codeblock) [![PHP Programming Language](https://camo.githubusercontent.com/9d445f874bf4fa21728f54be29e0482732f4d54bbcb46941f9d1c2c74d47f02b/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f7068702d762f72616d7365792f747769672d636f6465626c6f636b2e7376673f7374796c653d666c61742d73717561726526636f6c6f72423d253233383839324246)](https://php.net) [![Read License](https://camo.githubusercontent.com/baf7d4a585318de24731f66b977c9cde1d797bf62c086b634b29cb1658742bc5/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f72616d7365792f747769672d636f6465626c6f636b2e7376673f7374796c653d666c61742d73717561726526636f6c6f72423d6461726b6379616e)](https://github.com/ramsey/twig-codeblock/blob/main/LICENSE) [![Build Status](https://camo.githubusercontent.com/6edd77cbcc270af94949127a693dc886078855add5f0562a82e27664fae309da/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f72616d7365792f747769672d636f6465626c6f636b2f636f6e74696e756f75732d696e746567726174696f6e2e796d6c3f6272616e63683d6d61696e266c6f676f3d676974687562267374796c653d666c61742d737175617265)](https://github.com/ramsey/twig-codeblock/actions/workflows/continuous-integration.yml) [![Codecov Code Coverage](https://camo.githubusercontent.com/f7fe9d798ce1277cb94ec8e720894fed703950721fea153b3a848121ddb00a51/68747470733a2f2f696d672e736869656c64732e696f2f636f6465636f762f632f67682f72616d7365792f747769672d636f6465626c6f636b3f6c6162656c3d636f6465636f76266c6f676f3d636f6465636f76267374796c653d666c61742d737175617265)](https://codecov.io/gh/ramsey/twig-codeblock)

About
-----

[](#about)

Add code snippets with syntax highlighting and more to any [Twig](https://twig.symfony.com) template with ramsey/twig-codeblock, a port of the `{% codeblock %}`[Liquid tag for Octopress/Jekyll](https://github.com/octopress/codeblock).

ramsey/twig-codeblock includes an adapter for using [Pygments](http://pygments.org/), the Python syntax highlighter, with [ramsey/pygments](https://github.com/ramsey/pygments), but it may use any syntax highlighter. To use another syntax highlighter, implement `HighlighterInterface`(see below for an example).

This project adheres to a [code of conduct](CODE_OF_CONDUCT.md). By participating in this project and its community, you are expected to uphold this code.

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

[](#installation)

Install this package as a dependency using [Composer](https://getcomposer.org).

```
composer require ramsey/twig-codeblock
```

Usage
-----

[](#usage)

```
{% codeblock [attributes] %}
[lines of code]
{% endcodeblock %}

```

### Attributes

[](#attributes)

A number of attributes are available to `{% codeblock %}`:

AttributeExampleDescription`lang``lang:"php"`Tells the syntax highlighter the programming language being highlighted. Pass "plain" to disable highlighting.`title``title:"Figure 2."`Add a title to your code block.`link``link:"https://example.com"`Add a link to your code block title.`link_text``link_text:"Download Code"`Text to use for the link. Defaults to `"link"`.`linenos``linenos:false`Use `false` to disable line numbering. Defaults to `true`.`start``start:42`Start the line numbering in your code block at this value.`mark``mark:4-6,12`Mark specific lines of code. This example marks lines 4, 5, 6, and 12.`class``class:"myclass foo"`Add CSS class names to the code `` element.`format``format:"html"`The output format for the syntax highlighter. Defaults to "html."Tip

Order of attributes does not matter.

Warning

Not all highlighters will support all attributes. However, the Pygments highlighter does support each of these attributes.

### Example

[](#example)

```
{% codeblock lang:"php" %}
class Bar implements BarInterface
{
    private $baz;

    public function __construct(BazInterface $baz)
    {
        $this->baz = $baz;
    }

    public function doIt()
    {
        return $this->baz->do('it');
    }
}
{% endcodeblock %}
```

### Configuration

[](#configuration)

To use ramsey/twig-codeblock, create a `HighlighterReference` that defines the highlighter you want to use. If using `PygmentsHighlighter`, by default, it will look for `pygmentize` in your `PATH`.

```
use Ramsey\Twig\CodeBlock\CodeBlockExtension;
use Ramsey\Twig\CodeBlock\Highlighter\HighlighterReference;
use Ramsey\Twig\CodeBlock\Highlighter\PygmentsHighlighter;
use Twig\Environment;
use Twig\Loader\FilesystemLoader;

$reference = new HighlighterReference(PygmentsHighlighter::class);

$env = new Environment(new FilesystemLoader('/path/to/templates'));
$env->addExtension(new CodeBlockExtension($reference));
```

If `pygmentize` is not in the `PATH`, you may pass its location to the highlighter reference:

```
$reference = new HighlighterReference(
    PygmentsHighlighter::class,
    ['/path/to/pygmentize'],
);
```

Note

We use a `HighlighterReference` instead of an actual instance of `HighlighterInterface` because these values will be compiled into the Twig templates and cached for later execution.

### Pygments

[](#pygments)

This library provides `PygmentsHighlighter`, which depends on [ramsey/pygments](https://github.com/ramsey/pygments), but ramsey/pygments is not a dependency, since you may use other highlighters that implement `Ramsey\Twig\CodeBlock\Highlighter\HighlighterInterface`.

To use this library with ramsey/pygments, you must also require ramsey/pygments as a dependency:

```
composer require ramsey/pygments
```

Additionally, you will need to install [Python](https://www.python.org) and [Pygments](http://pygments.org/) and ensure the `pygmentize` CLI tool is available on your system. See the [Configuration](#configuration)section for help configuring Codeblock if `pygmentize` is not in your `PATH`.

```
pip install Pygments
```

#### Styles

[](#styles)

A syntax highlighter, such as Pygments, requires a stylesheet for the markup it generates. Pygments provides some stylesheets for you, which you may list from the command line:

```
pygmentize -L styles
```

To output and save one of these styles for use in your application, use something like:

```
pygmentize -S rainbow_dash -f html > rainbow_dash.css
```

Additionally, there are many custom Pygments styles found on the web, or you may create your own.

#### Languages

[](#languages)

If using Pygments, here are just a few of the languages (i.e., lexers) it supports:

- css
- diff
- html
- html+php
- javascript
- json
- php
- sass
- shell
- sql
- twig
- yaml

To see more, type the following from the command line:

```
pygmentize -L lexers
```

### Using your own highlighter

[](#using-your-own-highlighter)

If you have your own highlighter class that implements `Ramsey\Twig\CodeBlock\Highlighter\HighlighterInterface`, you may create a `HighlighterReference` using it. The array of values passed as the second argument will be passed to your class's constructor upon instantiation.

The arguments must be scalar values or arrays of scalar values, or they may be expressions that evaluate to scalar values or arrays of scalar values. Null values are also allowed. This restriction is because of the way these values are compiled into the Twig templates.

```
use Ramsey\Twig\CodeBlock\CodeBlockExtension;
use Ramsey\Twig\CodeBlock\Highlighter\HighlighterReference;
use Ramsey\Twig\CodeBlock\Highlighter\PygmentsHighlighter;
use Twig\Environment;
use Twig\Loader\FilesystemLoader;
use Your\Own\Highlighter as MyHighlighter;

$reference = new HighlighterReference(MyHighlighter::class, [$arg1, $arg2, $arg3]);

$env = new Environment(new FilesystemLoader('/path/to/templates'));
$env->addExtension(new CodeBlockExtension($reference));
```

Contributing
------------

[](#contributing)

Contributions are welcome! To contribute, please familiarize yourself with [CONTRIBUTING.md](CONTRIBUTING.md).

Coordinated Disclosure
----------------------

[](#coordinated-disclosure)

Keeping user information safe and secure is a top priority, and we welcome the contribution of external security researchers. If you believe you've found a security issue in software that is maintained in this repository, please read [SECURITY.md](SECURITY.md) for instructions on submitting a vulnerability report.

Copyright and License
---------------------

[](#copyright-and-license)

The ramsey/twig-codeblock library is copyright © [Ben Ramsey](https://benramsey.com/)and licensed for use under the MIT License (MIT). Please see [LICENSE](LICENSE)for more information.

###  Health Score

46

—

FairBetter than 93% of packages

Maintenance52

Moderate activity, may be stable

Popularity25

Limited adoption so far

Community13

Small or concentrated contributor base

Maturity80

Battle-tested with a long release history

 Bus Factor1

Top contributor holds 100% 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 ~915 days

Total

5

Last Release

436d ago

Major Versions

1.1.0 → 2.0.02017-04-18

2.0.0 → 3.0.02025-03-08

PHP version history (3 changes)1.0.0PHP &gt;=5.4.0

2.0.0PHP ^5.6 || ^7.0

3.0.0PHP ^8.2

### Community

Maintainers

![](https://www.gravatar.com/avatar/f514481fa9cfdfd5dbcaccb3e5b480f18ea05ebf331cddf9b1c5a2a96a78fef5?d=identicon)[ramsey](/maintainers/ramsey)

---

Top Contributors

[![ramsey](https://avatars.githubusercontent.com/u/42941?v=4)](https://github.com/ramsey "ramsey (54 commits)")

---

Tags

phppygmentizepygmentssyntax-highlightingtwigtwig-extensiontwigtwig-extensionsyntax-highlightingpygmentspygmentize

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Type Coverage Yes

### Embed Badge

![Health badge](/badges/ramsey-twig-codeblock/health.svg)

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

###  Alternatives

[twig/extra-bundle

A Symfony bundle for extra Twig extensions

91492.0M315](/packages/twig-extra-bundle)[twig/intl-extra

A Twig extension for Intl

36763.2M221](/packages/twig-intl-extra)[twig/string-extra

A Twig extension for Symfony String

22046.0M133](/packages/twig-string-extra)[symfony/ux-twig-component

Twig components for Symfony

21914.8M162](/packages/symfony-ux-twig-component)[bluetel-solutions/twig-truncate-extension

Twig Extension to truncate nested HTML, safely!

11100.0k](/packages/bluetel-solutions-twig-truncate-extension)

PHPackages © 2026

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