cypresslab/pygments-elephant-bundle
Installation
composer require cypresslab/pygments-elephant-bundle:0.*
Add the bundle to config/bundles.php in Symfony:
return [
// ...
Matteosister\PygmentsElephantBundle\MatteosisterPygmentsElephantBundle::class => ['all' => true],
];
Verify Pygments CLI Availability
Ensure pygmentize is installed and accessible in your system PATH. Test with:
pygmentize -V
First Use Case: Syntax Highlighting in Twig Inject the service in a Twig template or controller:
{{ highlight('php', '<?php echo "Hello"; ?>') }}
Or via controller:
$highlightedCode = $this->get('pygments.elephant')->highlight('php', '<?php echo "Hello"; ?>');
Dynamic Syntax Highlighting Use in Twig templates for code blocks:
{% for language, code in codeSnippets %}
<div class="code-block">
{{ highlight(language, code) }}
</div>
{% endfor %}
Service Integration
Inject the PygmentsElephant service in controllers/services:
public function __construct(PygmentsElephant $pygments) {
$this->pygments = $pygments;
}
public function showCodeExample() {
$highlighted = $this->pygments->highlight('javascript', 'console.log("Hi");');
return $this->render('example.html.twig', ['code' => $highlighted]);
}
Custom Lexers/Styles
Override default lexers/styles via configuration (see Gotchas for details):
# config/packages/matteosister_pygments_elephant.yaml
matteosister_pygments_elephant:
lexers_path: '%kernel.project_dir%/config/lexers'
styles_path: '%kernel.project_dir%/config/styles'
Batch Processing Process multiple code snippets in a loop:
$snippets = [
['php', '<?php echo "A"; ?>'],
['js', 'console.log("B");'],
];
$highlighted = array_map(
fn($snippet) => $this->pygments->highlight($snippet[0], $snippet[1]),
$snippets
);
Missing pygmentize CLI
CommandNotFoundException if pygmentize isn’t installed.easy_install pygments (Linux) or pip install pygments (cross-platform). On macOS, use brew install pygments.Lexer/Style Paths
config/packages/matteosister_pygments_elephant.yaml are correct and files are valid Pygments lexers/styles (.py files).Output Formatting
|raw filter sparingly or escape with |striptags if needed:
{{ highlight('html', '<div>')|raw }}
Performance
$cache = $this->get('cache.app');
$cacheKey = md5($language . $code);
$highlighted = $cache->get($cacheKey, function() use ($language, $code) {
return $this->pygments->highlight($language, $code);
});
pygmentize -L to list available lexers.try {
$highlighted = $this->pygments->highlight('unknown', 'code');
} catch (\Exception $e) {
$this->addFlash('error', 'Pygments error: ' . $e->getMessage());
}
Custom Lexers
Place .py lexer files in config/lexers and update the config path.
Pre/Post-Processing
Extend the bundle by overriding the PygmentsElephant service:
services:
app.pygments.elephant:
class: App\Service\CustomPygmentsElephant
decorates: pygments.elephant
arguments: ['@app.pygments.elephant.inner']
Symfony Flex Integration
For newer Symfony apps, manually register the bundle in config/bundles.php (no autoloading via Flex).
How can I help you explore Laravel packages today?