PHPackages                             machinateur/twig-comment-lexer - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Templating &amp; Views](/categories/templating)
4. /
5. machinateur/twig-comment-lexer

ActiveLibrary[Templating &amp; Views](/categories/templating)

machinateur/twig-comment-lexer
==============================

A custom twig lexer and extension to make comments available for parsing.

1.0.2(1y ago)02301MITPHPPHP &gt;=8.1.0

Since Apr 28Pushed 1y ago1 watchersCompare

[ Source](https://github.com/machinateur/twig-comment-lexer)[ Packagist](https://packagist.org/packages/machinateur/twig-comment-lexer)[ Docs](https://github.com/machinateur/twig-comment-lexer)[ RSS](/packages/machinateur-twig-comment-lexer/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependencies (2)Versions (4)Used By (1)

machinateur/twig-comment-lexer
==============================

[](#machinateurtwig-comment-lexer)

A custom [twig](https://github.com/twig/twig) lexer and extension to make comments available for parsing.

Comments like the one below are usually not available in twig for parsing. They're skipped.

```
{# some comment text #}

```

This package provides a lexer extension for twig, which makes comments available for node visitors! That means we can now implement parsing of twig comments, to extract template meta-data for certain applications!

Additionally, in debug mode, this extension exposes the `comment` tag in the compiled twig template class, which should make deep debugging easier by providing more information than only the line number (as done by default).

```
{% comment 'expose a comment to the raw template class' %}
{% comment 'no warranty if you break things, but */ works' %}

```

The value accepted by the `comment` tag is a constant string only, no interpolation, no nothing. Just a raw string. Quotes are not necessary for the native comment usage.

Requirements
------------

[](#requirements)

- PHP `>=8.1.0`
- Twig `^3.15`

Installation
------------

[](#installation)

Use composer to install this twig extension.

```
composer require machinateur/twig-comment-lexer
```

Intended usage
--------------

[](#intended-usage)

The lexer can be installed for an environment by calling `\Machinateur\Twig\Extension\CommentExtension::setLexer($twig)`, where `$twig` is the `\Twig\Environment` to set the lexer for. Have a look at the tests for an overview.

More out of necessity than as an actual feature, this twig extension allows for exposing comments in template code. This only works when in debug mode, for security reasons. Debug mode should not be active on production environments.

```
{% comment 'Comment test using real tag' %}
```

Which will become:

```
 /* comment on line 1
 Comment test using real tag
  */
```

Apart from that, this lexer extension allows for quite creative implementations. A more interesting way this can be used, is to extract template context based on comments (at compile time):

```
