Installation:
composer require carteni/highlight-bundle
Enable the Bundle in config/bundles.php:
return [
// ...
Carteni\HighlightBundle\CarteniHighlightBundle::class => ['all' => true],
];
Publish Configuration (optional):
php bin/console carteni:highlight:install
This generates config/packages/carteni_highlight.yaml with default settings.
First Use Case:
Use the highlight Twig filter to syntax-highlight code snippets:
{{ codeSnippet|highlight('php') }}
Outputs HTML-wrapped, syntax-highlighted code.
Resources/views/HighlightBundle/default.html.twig for default templates.config/packages/carteni_highlight.yaml for customization options (e.g., themes, languages).php bin/console list carteni to explore available commands (e.g., highlight:install).Highlighting Code in Twig:
{% set code = '<?php echo "Hello"; ?>' %}
{{ code|highlight('php', { 'theme': 'github' }) }}
language: Target language (e.g., php, javascript).options: Override defaults (e.g., theme, tabSize).Dynamic Language Detection:
Use the guessLanguage filter to auto-detect syntax:
{{ codeSnippet|highlight(codeSnippet|guessLanguage) }}
Custom Themes:
public/css/highlight/ (e.g., github.css).carteni_highlight:
themes:
github: '%kernel.project_dir%/public/css/highlight/github.css'
Laravel Blade:
Create a custom directive or use Twig via Blade::directive():
Blade::directive('highlight', function ($expression) {
return "<?php echo highlight($expression); ?>";
});
Usage:
@highlight($codeSnippet, 'php')
API Responses: Highlight code in JSON responses:
return response()->json([
'code' => highlight($request->code, 'python'),
]);
Caching: Cache highlighted output for static pages:
{% cache 'highlight_'.codeSnippet.md5 %}
{{ codeSnippet|highlight('ruby') }}
{% endcache %}
Outdated Dependencies:
scrivo/highlight.php (last updated 2017). Test thoroughly for edge cases (e.g., rare languages like coffeescript).Theme Loading:
public/css/highlight/. The bundle does not auto-download themes.composer post-install-cmd to fetch themes:
composer require --dev carteni/highlight-themes
Twig Auto-Reloading: Clear Twig cache after adding new themes:
php bin/console cache:clear
Language Support:
highlight.js languages are supported. Check vendor/scrivo/highlight.php/lib/languages/ for available options.config/packages/carteni_highlight.yaml:
carteni_highlight:
languages:
custom: '%kernel.project_dir%/path/to/custom.lang'
Invalid HTML Output:
highlight Twig filter is properly closed in templates. Use {{- ... -}} to trim whitespace if needed.Missing Styles:
public/ URL.Performance:
$highlighted = highlight($code, 'javascript');
$model->saveHighlightedCode($highlighted);
Custom Highlighter Service: Bind a custom service to override the default highlighter:
# config/services.yaml
services:
Carteni\HighlightBundle\HighlighterInterface: '@app.custom_highlighter'
Event Listeners: Trigger actions before/after highlighting via Symfony events (requires extending the bundle):
// Example: Log highlighted code
$eventDispatcher->addListener(
Carteni\HighlightBundle\Event\HighlightEvent::class,
function ($event) {
\Log::debug('Highlighted code:', ['language' => $event->getLanguage()]);
}
);
Language Auto-Loading: Dynamically load languages from a directory:
// In a bundle compiler pass
$definition->addMethodCall('loadLanguagesFromDir', ['%kernel.project_dir%/config/languages']);
How can I help you explore Laravel packages today?