Code highlighting
The Markup.Highlighter helper defaults to native PHP highlighting and needs no extra PHP library.
Load it in AppView::initialize():
$this->loadHelper('Markup.Highlighter');In a PHP template:
<?= $this->Highlighter->highlight('<?php $answer = 42;', ['lang' => 'php']) ?>Include the PHP opening tag so highlight_string() recognizes PHP syntax. lang sets the CSS class; it does not choose another native language parser. PHP 8.3 and later use a div wrapper because native highlighting already includes a pre element; older supported PHP uses the configured pre wrapper.
JavaScript highlighting
Select the JavaScript wrapper:
$this->loadHelper('Markup.Highlighter', [
'highlighter' => \Markup\Highlighter\JsHighlighter::class,
]);It returns escaped code inside pre and code elements, with a language- class. Add the browser library and CSS yourself, following Highlight.js or Prism instructions. The plugin does not load them or call their initialization functions.
<?= $this->Highlighter->highlight('const answer = 42;', ['lang' => 'javascript', 'tabToSpaces' => 2]) ?>tabToSpaces defaults to four for the JavaScript wrapper; zero leaves tabs intact. The escape option defaults to true. Disabling it allows raw HTML in the output.
Both highlighters support prefix, lang and templates. To change a wrapper:
$this->loadHelper('Markup.Highlighter', [
'highlighter' => \Markup\Highlighter\JsHighlighter::class,
'templates' => ['code' => '<div{{attr}}>{{content}}</div>'],
]);A custom implementation uses Markup\Highlighter\HighlighterInterface. The abstract Highlighter class supplies configuration and template support.