alto/twig-code-highlight
Composer 安装命令:
composer require alto/twig-code-highlight
包简介
Twig extension for Alto Code Highlight with code_highlight tag and filter
关键字:
README 文档
README
Twig extension for alto/code-highlight that adds a block tag and a filter for rendering syntax-highlighted code in templates.
{% code_highlight %}
#[AsTwigExtension]
public function getFilters(): array
{
return array_all(
);
}
{% endcode_highlight %}
Requirements
- PHP
^8.4(>= 8.4.0, < 9.0.0) - Twig
^3.23(>= 3.23.0, < 4.0.0)
Installation
composer require alto/twig-code-highlight
Register extension
use Alto\Twig\CodeHighlight\CodeHighlightExtension; use Alto\Twig\CodeHighlight\Runtime\CodeHighlightRuntime; use Twig\RuntimeLoader\FactoryRuntimeLoader; $extension = new CodeHighlightExtension(); $twig->addExtension($extension); $twig->addRuntimeLoader(new FactoryRuntimeLoader([ CodeHighlightRuntime::class => fn () => new CodeHighlightRuntime( $extension->getHighlighter(), $extension->getDefaultOptions(), ), ]));
Tag {% code_highlight %}
This tag highlights the enclosed code block and renders highlighted HTML output.
Example
{% code_highlight 'php' %}
<?php
echo "Hello world";
{% endcode_highlight %}
Options (arguments)
| Option | Type | Default | Behavior |
|---|---|---|---|
line_numbers |
bool | false |
Enables line numbers in output |
highlight_lines |
array | [] |
Only positive integers are used; other values are ignored |
Theme:
theme is not passable as a tag argument; configure the Highlighter theme when registering the extension.
See theme docs in the core library: https://github.com/phpalto/code-highlight.
Filter |code_highlight
This filter highlights a code string and returns highlighted HTML output.
Syntax:
{{ code|code_highlight(language, options) }}
Example
{{ snippet|code_highlight('javascript') }}
Options (arguments)
| Option | Type | Default | Behavior |
|---|---|---|---|
line_numbers |
bool | false |
Enables line numbers in output |
highlight_lines |
array | [] |
Only positive integers are used; other values are ignored |
Theme:
theme is not passable as a filter argument; configure the Highlighter theme when registering the extension.
See theme docs in the core library: https://github.com/phpalto/code-highlight.
Contributing
Contributions are welcome! Please feel free to submit issues or pull requests.
License
Released by the Alto project under the MIT License.
alto/twig-code-highlight 适用场景与选型建议
alto/twig-code-highlight 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2 次下载、GitHub Stars 达 0, 最近一次更新时间为 2026 年 04 月 17 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「twig」 「twig-extension」 「alto」 「syntax-highlighting」 「code-highlight」 「code-highlighting」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 alto/twig-code-highlight 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 alto/twig-code-highlight 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 alto/twig-code-highlight 相关的其它包
同方向 / 同关键字的高下载量 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
Adds exception handling to Twig
Adds a {{ plural }}, {{ name }}, {{ numeral }}, {{ ordinal }} and {{ money }} filters to Twig templating engine for Russian pluralization and declenation.
Caching and compression for Twig assets (JavaScript and CSS).
统计信息
- 总下载量: 2
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 42
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2026-04-17