devtheorem/php-handlebars-parser
Parse Handlebars templates to a spec-compliant AST with PHP.
Maintainers
Package info
github.com/devtheorem/php-handlebars-parser
pkg:composer/devtheorem/php-handlebars-parser
Requires
- php: >=8.2
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.95
- ircmaxell/php-yacc: ^0.0.8
- jbboehr/handlebars-spec: dev-master
- phpstan/phpstan: ^2.1.54
- phpunit/phpunit: ^11.5
Suggests
None
Provides
None
Conflicts
None
Replaces
None
MIT a45b34baeb7d5304787aa30380758177b4193773
- Theodore Brown <theodorejb.woop@outlook.com>
README
Parse Handlebars templates to a spec-compliant AST with PHP.
Implements the same lexical analysis and grammar specification as Handlebars.js, so any template which can (or cannot) be parsed by Handlebars.js should parse (or error) the same way here.
Note
This project is only a parser. To compile Handlebars templates to native PHP for execution, see PHP Handlebars, which uses this parser.
Installation
composer require devtheorem/php-handlebars-parser
Usage
use DevTheorem\HandlebarsParser\ParserFactory; $parser = (new ParserFactory())->create(); $template = "Hello {{name}}!"; $result = $parser->parse($template);
If the template contains invalid syntax, an exception will be thrown.
Otherwise, $result will contain a DevTheorem\HandlebarsParser\Ast\Program instance.
Whitespace handling
The ignoreStandalone Handlebars compilation option can be passed to parse():
$result = $parser->parse($template, ignoreStandalone: true);
Author
Theodore Brown
https://theodorejb.me
