alto/twig-code-highlight

Twig extension for Alto Code Highlight with code_highlight tag and filter

Maintainers

Package info

github.com/PhpAlto/twig-code-highlight

pkg:composer/alto/twig-code-highlight

Fund package maintenance!

smnandre

Statistics

Installs: 2

Dependents: 0

Suggesters: 0

Stars: 0

Open Issues: 0

dev-main 2026-04-06 03:16 UTC

This package is auto-updated.

Last update: 2026-04-17 01:15:57 UTC


README

Twig extension for alto/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

  • PHP ^8.4 (>= 8.4.0, < 9.0.0)
  • Twig ^3.23 (>= 3.23.0, < 4.0.0)

Installation

composer require alto/twig-code-highlight

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 %}

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

Example

{% code_highlight 'php' %}
<?php
echo "Hello world";
{% endcode_highlight %}

Options (arguments)

Option Type Default Behavior
line_numbers bool false Enables line numbers in output
highlight_lines array [] Only positive integers are used; other values are ignored

Theme:

theme is not passable as a tag argument; configure the Highlighter theme when registering the extension.

See theme docs in the core library: https://github.com/phpalto/code-highlight.

Filter |code_highlight

This filter highlights a code string and returns highlighted HTML output.

Syntax:

{{ code|code_highlight(language, options) }}

Example

{{ snippet|code_highlight('javascript') }}

Options (arguments)

Option Type Default Behavior
line_numbers bool false Enables line numbers in output
highlight_lines array [] Only positive integers are used; other values are ignored

Theme:

theme is not passable as a filter argument; configure the Highlighter theme when registering the extension.

See theme docs in the core library: https://github.com/phpalto/code-highlight.

Contributing

Contributions are welcome! Please feel free to submit issues or pull requests.

License

Released by the Alto project under the MIT License.