Download the PHP package machinateur/twig-comment-lexer without Composer
On this page you can find all versions of the php package machinateur/twig-comment-lexer. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download machinateur/twig-comment-lexer
More information about machinateur/twig-comment-lexer
Files in machinateur/twig-comment-lexer
Package twig-comment-lexer
Short Description A custom twig lexer and extension to make comments available for parsing.
License MIT
Homepage https://github.com/machinateur/twig-comment-lexer
Informations about the package twig-comment-lexer
machinateur/twig-comment-lexer
A custom 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.
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).
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
- PHP
>=8.1.0
- Twig
^3.15
Installation
Use composer to install this twig extension.
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.
Which will become:
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):
Useful reading, regarding collecting template data:
An example on how one can expose compile-time information at runtime,
is the machinateur/twig-context-tags
library.
Limitations and compatibility
Since this library is quite dependent on private code structure of twig's own lexer class. So it could break easily when internals change with newer versions.
Tests
Run tests using the composer script:
License
It's MIT.