brotkrueml/twig-codehighlight
Composer 安装命令:
composer require brotkrueml/twig-codehighlight
包简介
Twig extension for code highlighting using highlight.php
关键字:
README 文档
README
This package provides a Twig extension for server-side code highlighting. Under the hood, the scrivo/highlight.php package is used which does the hard work. You can use every theme provided for highlight.js.
An addition to the highlighting of code this Twig extension provides additional (opinionated) features:
- language aliases
- additional languages
- line numbers
- emphasize lines
- classes
- list of available languages
Usage
Add the extension to the Twig environment:
$twig->addExtension(new Brotkrueml\TwigCodeHighlight\Extension());
Use it in Twig templates:
{{ "<?php echo 'test'; ?>" | codehighlight("php") }}
If the language is not available, a raw code block is displayed.
It is also possible to inject a logger that implements \Psr\Log\LoggerInterface
to display warnings when a given language is not available, either via dependency
injection or manually:
$twig->addExtension(new Brotkrueml\TwigCodeHighlight\Extension($myLogger));
You can also use named arguments, the example above can be also written like:
{{ "<?php echo 'test'; ?>" | codehighlight(language="php") }}
This will render something like this:
<pre><code class="hljs php"><span class="hljs-meta"><?php</span> <span class="hljs-keyword">echo</span> <span class="hljs-string">"test"</span>; <span class="hljs-meta">?></span></code></pre>
Language aliases
When you have already an existing application with languages named alternatively than highlight.php provides them, you can assign an array of language aliases when instantiating the extension class:
$twig->addExtension(new Brotkrueml\TwigCodeHighlight\Extension(languageAliases: ['text' => 'plaintext', 'sh' => 'shell']));
In this example, we introduce text as an alias for plaintext and sh for shell.
Additional languages
Sometimes you have the need to add languages which are not shipped by the
scrivo/highlight.php package. You can add one or more custom languages:
$twig->addExtension(new Brotkrueml\TwigCodeHighlight\Extension( additionalLanguages: [ ['custom_language', '/path/to/the/custom_language.json'], ['another_language', '/path/to/the/another_language.json', true], ] ));
The array consists of the following values:
- The language ID (here:
custom_languageandanother_language) - required - The full path to the language (here:
/path/to/the/custom_language.jsonand/path/to/the/another_language.json) - required - Should this language override a provided one (default:
false, set totrueif it should override) - optional
Line numbers
By default, no line numbers are displayed. You can switch them one by setting the second argument:
Use it in Twig templates:
{{ "<?php echo 'test'; ?>" | codehighlight(language="php", showLineNumbers=true) }}
Line numbers start with 1, but can also give a custom start number with another argument:
{{ "<?php echo 'test'; ?>" | codehighlight(language="php", showLineNumbers=true, startWithLineNumber=11) }}
This adds a <span data-line-number="x">...</span> to each line, where x is the increasing line number.
You can then use a CSS rule to display the line number, for example:
code [data-line-number]::before { content: attr(data-line-number); display: inline-block; margin-right: 1em; text-align: right; width: 2ch; }
Emphasize lines
You can emphasize lines which highlights one or more lines in a code snippet.
Use it in Twig templates:
{{ code | codehighlight(language="php", emphasizeLines="1-3,5") }}
This example emphasizes the lines 1,2,3 and 5.
This adds a <span data-emphasize-line>...</span> to each line which should be emphasized.
You can then use a custom CSS rule to highlight the line, for example:
code [data-emphasize-line] { background: lightcyan; }
Classes
There are two ways to set or more classes to the <pre> tag:
-
To set the classes in an application use the
classesconstructor argument when instantiating the Twig extension:$twig->addExtension(new Brotkrueml\TwigCodeHighlight\Extension(classes: 'some-default-class'));
Which results in the following HTML code:
<pre class="some-default-class">...</pre>
-
You can add one or more additional classes to the
<pre>tag for a special code block:{{ some text | codehighlight(language="plaintext", classes="some-special-class another-special-class") }}Which results in the following HTML code:
<pre class="some-special-class another-special-class"><code class="hljs plaintext">some text</code></pre>
Using both variants together results in the following HTML code:
<pre class="some-default-class some-special-class another-special-class"><code class="hljs plaintext">some text</code></pre>
List of available languages
Sometimes it can be useful to provide a list of available languages. The function codehighlight_languages()
is available to output such a list:
<ul> {% for language in codehighlight_languages() %} <li>{{ language }}</li> {% endfor %} </ul>
brotkrueml/twig-codehighlight 适用场景与选型建议
brotkrueml/twig-codehighlight 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 12.85k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2024 年 01 月 28 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「twig」 「Syntax highlighter」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 brotkrueml/twig-codehighlight 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 brotkrueml/twig-codehighlight 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 brotkrueml/twig-codehighlight 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Shoot aims to make providing data to your templates more manageable
This Symfony bundle integrates PhpSpreadsheet into Symfony using Twig.
A Twig extension to insert css as inline styles with a tag
Bookdown.io With Bootswatch Styles And Prism Syntax Highlighting
Caching and compression for Twig assets (JavaScript and CSS).
PHP code highlighter
统计信息
- 总下载量: 12.85k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 26
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: GPL-2.0-or-later
- 更新时间: 2024-01-28