ramsey / twig-codeblock
A Twig extension for defining blocks of code for syntax highlighting (with Pygments) and more.
Requires
- php: ^5.6 || ^7.0
- ramsey/pygments: ^1.0
- twig/twig: ^1.15 || ^2.0
Requires (Dev)
- jakub-onderka/php-parallel-lint: ^0.9.2
- mockery/mockery: ^0.9.9
- phpunit/phpunit: ^5.7
- squizlabs/php_codesniffer: ^2.8
This package is auto-updated.
Last update: 2024-10-29 04:19:58 UTC
README
Add code snippets with syntax highlighting and more to any Twig template.
The Codeblock extension for Twig is a port of the {% codeblock %} liquid tag for Octopress/Jekyll.
By default, Codeblock uses Pygments, the Python syntax highlighter, to generate HTML markup suitable for highlighting blocks of code, but it may use any syntax highlighter. To use another syntax highlighter, simply implement HighlighterInterface
(see below for an example).
This project adheres to a Contributor Code of Conduct. By participating in this project and its community, you are expected to uphold this code.
Installation
The preferred method of installation is via Packagist and Composer. Run the following command to install the package and add it as a requirement to your project's composer.json
:
composer require ramsey/twig-codeblock
Usage
{% codeblock [options] %}
[lines of code]
{% endcodeblock %}
Options
A number of options are available to Codeblock. Note that order does not matter.
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
By default, Codeblock uses Pygments and, if pygmentize
is in your PATH
, then you do not need to pass any arguments.
use Ramsey\Twig\CodeBlock\CodeBlockExtension; $env = new \Twig_Environment(new \Twig_Loader_Filesystem('/path/to/templates')); $env->addExtension(new CodeBlockExtension());
If pygmentize
is not in the PATH
, you may specify its location:
use Ramsey\Twig\CodeBlock\CodeBlockExtension; $env = new \Twig_Environment(new \Twig_Loader_Filesystem('/path/to/templates')); $env->addExtension( new CodeBlockExtension('pygments', ['/usr/local/bin/pygmentize']) );
Pygments
By default, Pygments is used for highlighting code. You will need to install Pygments and ensure that the pygmentize
CLI tool is available on your system. See the Configuration section for help configuring Codeblock if pygmentize
is not in your PATH
.
pip install Pygments
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:
pygmentize -S solarizedlight -f html > solarizedlight.css
Additionally, there are many custom Pygments styles found on the web, and you may create your own.
Languages
If using Pygments, here are just a few of the languages (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
If you have your own highlighter class that implements Ramsey\Twig\CodeBlock\Highlighter\HighlighterInterface
, then you may specify the fully-qualified classname as the first argument to the extension. The second argument is an array of 0-indexed values that will be passed as arguments to your class constructor. Make sure that you specify them in the correct order as your constructor requires.
use Ramsey\Twig\CodeBlock\CodeBlockExtension; use Your\Own\Highlighter as MyHighlighter; $env = new \Twig_Environment(new \Twig_Loader_Filesystem('/path/to/templates')); $env->addExtension( new CodeBlockExtension(MyHighlighter::class, ['arg1', 'arg2', 'arg3']) );
Contributing
Contributions are welcome! Please read CONTRIBUTING for details.
Copyright and license
The ramsey/twig-codeblock library is copyright © Ben Ramsey and licensed for use under the MIT License (MIT). Please see LICENSE for more information.