PHPackages                             alto/twig-code-highlight - 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. alto/twig-code-highlight

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

alto/twig-code-highlight
========================

Twig extension for Alto Code Highlight with code\_highlight tag and filter

02PHPCI failing

Since Apr 6Pushed 2mo agoCompare

[ Source](https://github.com/PhpAlto/twig-code-highlight)[ Packagist](https://packagist.org/packages/alto/twig-code-highlight)[ RSS](/packages/alto-twig-code-highlight/feed)WikiDiscussions main Synced 1w ago

READMEChangelogDependenciesVersions (1)Used By (0)

Alto Twig Code Highlight
========================

[](#alto-twig-code-highlight)

Twig extension for [alto/code-highlight](https://github.com/phpalto/code-highlight) that adds a block tag and a filter for rendering syntax-highlighted code in templates.

```
{% code_highlight %}
   #[AsTwigExtension]
   public function getFilters(): array
   {
        return array_all(

        );
   }
{% endcode_highlight %}
```

Requirements
------------

[](#requirements)

- PHP `^8.4` (&gt;= 8.4.0, &lt; 9.0.0)
- Twig `^3.23` (&gt;= 3.23.0, &lt; 4.0.0)

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

[](#installation)

```
composer require alto/twig-code-highlight
```

Register extension
------------------

[](#register-extension)

```
use Alto\Twig\CodeHighlight\CodeHighlightExtension;
use Alto\Twig\CodeHighlight\Runtime\CodeHighlightRuntime;
use Twig\RuntimeLoader\FactoryRuntimeLoader;

$extension = new CodeHighlightExtension();
$twig->addExtension($extension);

$twig->addRuntimeLoader(new FactoryRuntimeLoader([
    CodeHighlightRuntime::class => fn () => new CodeHighlightRuntime(
        $extension->getHighlighter(),
        $extension->getDefaultOptions(),
    ),
]));
```

Tag `{% code_highlight %}`
--------------------------

[](#tag--code_highlight-)

This tag highlights the enclosed code block and renders highlighted HTML output.

### Example

[](#example)

```
{% code_highlight 'php' %}
