承接 alto/twig-code-highlight 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

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.

统计信息

  • 总下载量: 2
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 6
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-04-17

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固