adiog/syntax-highlight-bundle
Symfony bundle that packages Alex Gorbatchev’s SyntaxHighlighter 3.0.83 for Composer-based apps. Install via composer, enable the bundle in AppKernel, then run assets:install to publish the JS/CSS for client-side code highlighting.
Installation:
composer require adiog/syntax-highlight-bundle:dev-master
Add the bundle to app/AppKernel.php:
new Adiog\SyntaxHighlightBundle\SyntaxHighlightBundle(),
Install Assets:
php bin/console assets:install --symlink
This symlinks the required JS/CSS files (shCore.js, shTheme*.css) from vendor/adiog/syntax-highlight-bundle/Resources/public/ to web/bundles/adiog/.
First Use Case: Embed code in a Twig template with syntax highlighting:
{{ syntax_highlight(code, 'php') }}
Where code is a string (e.g., $code = '<?php echo "Hello"; ?>';).
Adiog\SyntaxHighlightBundle\Twig\SyntaxHighlightExtension for available filters/tags.Default, Dracula, or Eclipse. Override in config.yml:
adiog_syntax_highlight:
theme: 'Dracula'
SyntaxHighlighter supports languages like php, javascript, css, sql, etc. (see original docs).Dynamic Code Highlighting:
{% set code = 'function foo() { return "bar"; }' %}
<pre>{{ syntax_highlight(code, 'javascript') }}</pre>
return $this->render('page.html.twig', ['code' => $rawCode]);
Reusing Highlighted Code:
// src/Service/CodeCache.php
class CodeCache {
public function getHighlighted(string $code, string $lang): string {
// Implement caching logic (e.g., Redis, filesystem).
}
}
Custom Themes:
Resources/public/shTheme*.css to your assets and updating the config:
adiog_syntax_highlight:
theme_path: '%kernel.root_dir%/../web/css/custom-theme.css'
Integration with Markdown:
php-markdown) to highlight code blocks:
$markdown = new Markdown();
$html = $markdown->transform($text);
// Post-process to highlight code blocks with syntax_highlight.
Asset Management:
web/ to avoid symlink issues:
mkdir -p web/bundles/adiog
cp -r vendor/adiog/syntax-highlight-bundle/Resources/public/* web/bundles/adiog/
.gitignore:
/web/bundles/adiog/
Lazy Loading:
{% if codeBlocks|length > 0 %}
{{ parent() }} {# Load assets via parent template #}
{% endif %}
API Responses:
return new JsonResponse([
'code' => $highlightedCode,
'language' => 'php',
]);
Asset Paths:
assets:install post-install.composer.json:
"scripts": {
"post-install-cmd": [
"Incenteev\\ParameterHandler\\ScriptHandler::buildParameters",
"Adiog\\SyntaxHighlightBundle\\Composer\\InstallAssets::postInstall"
]
}
Language Mismatches:
'php5' instead of 'php') may break highlighting.Twig Filter vs. Function:
syntax_highlight filter and function. Use the filter for inline highlighting:
{{ code|syntax_highlight('php') }} {# Filter #}
{{ syntax_highlight(code, 'php') }} {# Function #}
Deprecated Bundle:
SyntaxHighlighter (v3.0.83) is 14 years old and lacks modern features (e.g., ES6, TypeScript).highlight.js) if needed, but this bundle may suffice for legacy codebases.Broken Highlighting:
shCore.js or theme CSS.theme config points to a valid file.Console Errors:
shCore.js is loaded after jQuery (required by the highlighter):
{{ parent() }} {# Loads assets in correct order #}
Twig Errors:
config.yml if missing:
twig:
globals:
syntax_highlight: '@adiog_syntax_highlight.twig.syntax_highlight'
Custom Languages:
shBrush*.js from the original library to your bundle’s Resources/public/ and update the config:
adiog_syntax_highlight:
custom_brushes: ['shBrushCustom.js']
Pre/Post-Processing:
// src/Adiog/SyntaxHighlightBundle/Twig/ExtendedSyntaxHighlightExtension.php
class ExtendedSyntaxHighlightExtension extends \Adiog\SyntaxHighlightBundle\Twig\SyntaxHighlightExtension {
public function getSyntaxHighlightedCode($code, $language = null) {
if (null === $language) {
$language = $this->detectLanguage($code);
}
return parent::getSyntaxHighlightedCode($code, $language);
}
}
Register it in services.yml:
services:
adiog_syntax_highlight.twig.syntax_highlight:
class: Adiog\SyntaxHighlightBundle\Twig\ExtendedSyntaxHighlightExtension
tags: ['twig.extension']
Configuration Overrides:
config_dev.yml) to switch themes:
adiog_syntax_highlight:
theme: '%kernel.debug% ? Default : Dracula'
How can I help you explore Laravel packages today?