{"id":22288934,"url":"https://github.com/brotkrueml/twig-codehighlight","last_synced_at":"2026-03-01T04:09:17.532Z","repository":{"id":207044655,"uuid":"718296597","full_name":"brotkrueml/twig-codehighlight","owner":"brotkrueml","description":"Twig extension for code highlighting using highlight.php","archived":false,"fork":false,"pushed_at":"2025-09-24T18:59:22.000Z","size":63,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-11-18T12:36:01.293Z","etag":null,"topics":["codehighlight","codehighlighter","codehighlighting","twig-extension"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"gpl-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/brotkrueml.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2023-11-13T19:45:51.000Z","updated_at":"2025-09-24T18:59:26.000Z","dependencies_parsed_at":"2024-02-02T18:44:23.325Z","dependency_job_id":"c7c5e5d2-5173-40aa-bbf6-04e83092f95e","html_url":"https://github.com/brotkrueml/twig-codehighlight","commit_stats":null,"previous_names":["brotkrueml/code-highlight-twig-extension","brotkrueml/twig-codehighlight"],"tags_count":3,"template":false,"template_full_name":null,"purl":"pkg:github/brotkrueml/twig-codehighlight","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/brotkrueml%2Ftwig-codehighlight","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/brotkrueml%2Ftwig-codehighlight/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/brotkrueml%2Ftwig-codehighlight/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/brotkrueml%2Ftwig-codehighlight/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/brotkrueml","download_url":"https://codeload.github.com/brotkrueml/twig-codehighlight/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/brotkrueml%2Ftwig-codehighlight/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":29960236,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-03-01T01:47:18.291Z","status":"online","status_checked_at":"2026-03-01T02:00:07.437Z","response_time":124,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["codehighlight","codehighlighter","codehighlighting","twig-extension"],"created_at":"2024-12-03T17:07:37.513Z","updated_at":"2026-03-01T04:09:17.519Z","avatar_url":"https://github.com/brotkrueml.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Twig extension for code highlighting\n\n[![CI](https://github.com/brotkrueml/twig-codehighlight/actions/workflows/ci.yml/badge.svg)](https://github.com/brotkrueml/twig-codehighlight/actions/workflows/ci.yml)\n\nThis package provides a Twig extension for server-side code highlighting. Under the\nhood, the [scrivo/highlight.php](https://github.com/scrivo/highlight.php) package is\nused which does the hard work. You can use every\n[theme provided for highlight.js](https://highlightjs.org/demo).\n\nAn addition to the highlighting of code this Twig extension provides additional\n(opinionated) features:\n\n- [language aliases](#language-aliases)\n- [additional languages](#additional-languages)\n- [line numbers](#line-numbers)\n- [emphasize lines](#emphasize-lines)\n- [classes](#classes)\n- [list of available languages](#list-of-available-languages)\n\n## Usage\n\nAdd the extension to the Twig environment:\n\n```php\n$twig-\u003eaddExtension(new Brotkrueml\\TwigCodeHighlight\\Extension());\n```\n\nUse it in Twig templates:\n```twig\n{{ \"\u003c?php echo 'test'; ?\u003e\" | codehighlight(\"php\") }}\n```\n\nIf the language is not available, a raw code block is displayed.\n\nIt is also possible to inject a logger that implements `\\Psr\\Log\\LoggerInterface`\nto display warnings when a given language is not available, either via dependency\ninjection or manually:\n\n```php\n$twig-\u003eaddExtension(new Brotkrueml\\TwigCodeHighlight\\Extension($myLogger));\n```\n\nYou can also use named arguments, the example above can be also written like:\n\n```twig\n{{ \"\u003c?php echo 'test'; ?\u003e\" | codehighlight(language=\"php\") }}\n```\n\nThis will render something like this:\n\n```html\n\u003cpre\u003e\u003ccode class=\"hljs php\"\u003e\u003cspan class=\"hljs-meta\"\u003e\u0026lt;?php\u003c/span\u003e \u003cspan class=\"hljs-keyword\"\u003eecho\u003c/span\u003e \u003cspan class=\"hljs-string\"\u003e\"test\"\u003c/span\u003e; \u003cspan class=\"hljs-meta\"\u003e?\u0026gt;\u003c/span\u003e\u003c/code\u003e\u003c/pre\u003e\n```\n\n### Language aliases\n\nWhen you have already an existing application with languages named alternatively than highlight.php\nprovides them, you can assign an array of language aliases when instantiating the extension class:\n\n```php\n$twig-\u003eaddExtension(new Brotkrueml\\TwigCodeHighlight\\Extension(languageAliases: ['text' =\u003e 'plaintext', 'sh' =\u003e 'shell']));\n```\n\nIn this example, we introduce `text` as an alias for `plaintext` and `sh` for `shell`.\n\n\n### Additional languages\n\nSometimes you have the need to add languages which are not shipped by the\n`scrivo/highlight.php` package. You can add one or more custom languages:\n\n```php\n$twig-\u003eaddExtension(new Brotkrueml\\TwigCodeHighlight\\Extension(\n    additionalLanguages: [\n        ['custom_language', '/path/to/the/custom_language.json'],\n        ['another_language', '/path/to/the/another_language.json', true],\n    ]\n));\n```\n\nThe array consists of the following values:\n\n- The language ID (here: `custom_language` and `another_language`) - required\n- The full path to the language (here: `/path/to/the/custom_language.json` and `/path/to/the/another_language.json`) - required\n- Should this language override a provided one (default: `false`, set to `true` if it should override) - optional\n\n\n### Line numbers\n\nBy default, no line numbers are displayed. You can switch them one by setting the second argument:\n\nUse it in Twig templates:\n```twig\n{{ \"\u003c?php echo 'test'; ?\u003e\" | codehighlight(language=\"php\", showLineNumbers=true) }}\n```\n\nLine numbers start with `1`, but can also give a custom start number with another argument:\n\n```twig\n{{ \"\u003c?php echo 'test'; ?\u003e\" | codehighlight(language=\"php\", showLineNumbers=true, startWithLineNumber=11) }}\n```\n\nThis adds a `\u003cspan data-line-number=\"x\"\u003e...\u003c/span\u003e` to each line, where `x` is the increasing line number.\n\nYou can then use a CSS rule to display the line number, for example:\n\n```css\ncode [data-line-number]::before {\n    content: attr(data-line-number);\n    display: inline-block;\n    margin-right: 1em;\n    text-align: right;\n    width: 2ch;\n}\n```\n\n### Emphasize lines\n\nYou can emphasize lines which highlights one or more lines in a code snippet.\n\nUse it in Twig templates:\n```twig\n{{ code | codehighlight(language=\"php\", emphasizeLines=\"1-3,5\") }}\n```\n\nThis example emphasizes the lines 1,2,3 and 5.\n\nThis adds a `\u003cspan data-emphasize-line\u003e...\u003c/span\u003e` to each line which should be emphasized.\n\nYou can then use a custom CSS rule to highlight the line, for example:\n\n```css\ncode [data-emphasize-line] {\n    background: lightcyan;\n}\n```\n\n### Classes\n\nThere are two ways to set or more classes to the `\u003cpre\u003e` tag:\n\n1.  To set the classes in an application use the `classes` constructor argument when instantiating the\n    Twig extension:\n\n    ```php\n    $twig-\u003eaddExtension(new Brotkrueml\\TwigCodeHighlight\\Extension(classes: 'some-default-class'));\n    ```\n\n    Which results in the following HTML code:\n\n    ```html\n    \u003cpre class=\"some-default-class\"\u003e...\u003c/pre\u003e\n    ```\n\n2. You can add one or more additional classes to the `\u003cpre\u003e` tag for a special code block:\n\n    ```twig\n    {{ some text | codehighlight(language=\"plaintext\", classes=\"some-special-class another-special-class\") }}\n    ```\n\n    Which results in the following HTML code:\n\n    ```html\n    \u003cpre class=\"some-special-class another-special-class\"\u003e\u003ccode class=\"hljs plaintext\"\u003esome text\u003c/code\u003e\u003c/pre\u003e\n    ```\n\nUsing both variants together results in the following HTML code:\n\n```html\n\u003cpre class=\"some-default-class some-special-class another-special-class\"\u003e\u003ccode class=\"hljs plaintext\"\u003esome text\u003c/code\u003e\u003c/pre\u003e\n```\n\n### List of available languages\n\nSometimes it can be useful to provide a list of available languages. The function `codehighlight_languages()`\nis available to output such a list:\n\n```twig\n\u003cul\u003e\n{% for language in codehighlight_languages() %}\n    \u003cli\u003e{{ language }}\u003c/li\u003e\n{% endfor %}\n\u003c/ul\u003e\n```\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbrotkrueml%2Ftwig-codehighlight","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbrotkrueml%2Ftwig-codehighlight","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbrotkrueml%2Ftwig-codehighlight/lists"}