PHPackages                             brotkrueml/twig-codehighlight - 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. brotkrueml/twig-codehighlight

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

brotkrueml/twig-codehighlight
=============================

Twig extension for code highlighting using highlight.php

v1.0.0(1y ago)010.0k↓27.4%1GPL-2.0-or-laterPHPPHP &gt;= 8.1CI passing

Since Jan 28Pushed 7mo ago1 watchersCompare

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

READMEChangelog (3)Dependencies (9)Versions (4)Used By (1)

Twig extension for code highlighting
====================================

[](#twig-extension-for-code-highlighting)

[![CI](https://github.com/brotkrueml/twig-codehighlight/actions/workflows/ci.yml/badge.svg)](https://github.com/brotkrueml/twig-codehighlight/actions/workflows/ci.yml)

This package provides a Twig extension for server-side code highlighting. Under the hood, the [scrivo/highlight.php](https://github.com/scrivo/highlight.php) package is used which does the hard work. You can use every [theme provided for highlight.js](https://highlightjs.org/demo).

An addition to the highlighting of code this Twig extension provides additional (opinionated) features:

- [language aliases](#language-aliases)
- [additional languages](#additional-languages)
- [line numbers](#line-numbers)
- [emphasize lines](#emphasize-lines)
- [classes](#classes)
- [list of available languages](#list-of-available-languages)

Usage
-----

[](#usage)

Add the extension to the Twig environment:

```
$twig->addExtension(new Brotkrueml\TwigCodeHighlight\Extension());
```

Use it in Twig templates:

```
{{ "" | codehighlight("php") }}
```

If the language is not available, a raw code block is displayed.

It is also possible to inject a logger that implements `\Psr\Log\LoggerInterface`to display warnings when a given language is not available, either via dependency injection or manually:

```
$twig->addExtension(new Brotkrueml\TwigCodeHighlight\Extension($myLogger));
```

You can also use named arguments, the example above can be also written like:

```
{{ "" | codehighlight(language="php") }}
```

This will render something like this:

```
&lt;?php echo "test"; ?&gt;
```

### Language aliases

[](#language-aliases)

When you have already an existing application with languages named alternatively than highlight.php provides them, you can assign an array of language aliases when instantiating the extension class:

```
$twig->addExtension(new Brotkrueml\TwigCodeHighlight\Extension(languageAliases: ['text' => 'plaintext', 'sh' => 'shell']));
```

In this example, we introduce `text` as an alias for `plaintext` and `sh` for `shell`.

### Additional languages

[](#additional-languages)

Sometimes you have the need to add languages which are not shipped by the `scrivo/highlight.php` package. You can add one or more custom languages:

```
$twig->addExtension(new Brotkrueml\TwigCodeHighlight\Extension(
    additionalLanguages: [
        ['custom_language', '/path/to/the/custom_language.json'],
        ['another_language', '/path/to/the/another_language.json', true],
    ]
));
```

The array consists of the following values:

- The language ID (here: `custom_language` and `another_language`) - required
- The full path to the language (here: `/path/to/the/custom_language.json` and `/path/to/the/another_language.json`) - required
- Should this language override a provided one (default: `false`, set to `true` if it should override) - optional

### Line numbers

[](#line-numbers)

By default, no line numbers are displayed. You can switch them one by setting the second argument:

Use it in Twig templates:

```
{{ "" | codehighlight(language="php", showLineNumbers=true) }}
```

Line numbers start with `1`, but can also give a custom start number with another argument:

```
{{ "" | codehighlight(language="php", showLineNumbers=true, startWithLineNumber=11) }}
```

This adds a `...` to each line, where `x` is the increasing line number.

You can then use a CSS rule to display the line number, for example:

```
code [data-line-number]::before {
    content: attr(data-line-number);
    display: inline-block;
    margin-right: 1em;
    text-align: right;
    width: 2ch;
}
```

### Emphasize lines

[](#emphasize-lines)

You can emphasize lines which highlights one or more lines in a code snippet.

Use it in Twig templates:

```
{{ code | codehighlight(language="php", emphasizeLines="1-3,5") }}
```

This example emphasizes the lines 1,2,3 and 5.

This adds a `...` to each line which should be emphasized.

You can then use a custom CSS rule to highlight the line, for example:

```
code [data-emphasize-line] {
    background: lightcyan;
}
```

### Classes

[](#classes)

There are two ways to set or more classes to the `` tag:

1. To set the classes in an application use the `classes` constructor argument when instantiating the Twig extension:

    ```
    $twig->addExtension(new Brotkrueml\TwigCodeHighlight\Extension(classes: 'some-default-class'));
    ```

    Which results in the following HTML code:

    ```
    ...
    ```
2. You can add one or more additional classes to the `` tag for a special code block:

    ```
    {{ some text | codehighlight(language="plaintext", classes="some-special-class another-special-class") }}
    ```

    Which results in the following HTML code:

    ```
    some text
    ```

Using both variants together results in the following HTML code:

```
some text
```

### List of available languages

[](#list-of-available-languages)

Sometimes it can be useful to provide a list of available languages. The function `codehighlight_languages()`is available to output such a list:

```

{% for language in codehighlight_languages() %}
    {{ language }}
{% endfor %}

```

###  Health Score

37

—

LowBetter than 83% of packages

Maintenance51

Moderate activity, may be stable

Popularity24

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity52

Maturing project, gaining track record

 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 ~133 days

Total

3

Last Release

575d ago

Major Versions

v0.1.1 → v1.0.02024-10-20

### Community

Maintainers

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

---

Top Contributors

[![brotkrueml](https://avatars.githubusercontent.com/u/2566282?v=4)](https://github.com/brotkrueml "brotkrueml (40 commits)")

---

Tags

codehighlightcodehighlightercodehighlightingtwig-extensiontwigSyntax highlighter

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan, Rector

Type Coverage Yes

### Embed Badge

![Health badge](/badges/brotkrueml-twig-codehighlight/health.svg)

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

###  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)[rcrowe/twigbridge

Adds the power of Twig to Laravel

9105.9M50](/packages/rcrowe-twigbridge)[twig/string-extra

A Twig extension for Symfony String

22046.0M133](/packages/twig-string-extra)[twig/cssinliner-extra

A Twig extension to allow inlining CSS

22918.5M55](/packages/twig-cssinliner-extra)[symfony/ux-twig-component

Twig components for Symfony

21914.8M162](/packages/symfony-ux-twig-component)

PHPackages © 2026

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