mcuelenaere / plitz
Pure PHP port of Blitz
Installs: 7 999
Dependents: 0
Suggesters: 0
Security: 0
Stars: 2
Watchers: 4
Forks: 2
Open Issues: 1
Requires
- php: >=5.5
Requires (Dev)
- mockery/mockery: ~0.9
- phpunit/phpunit: ~4.0
- satooshi/php-coveralls: ~0.6
- symfony/yaml: ~2.6
This package is auto-updated.
Last update: 2024-10-29 05:08:41 UTC
README
Plitz is a pure PHP port of the Blitz PHP template extension.
Installation
Install with composer:
composer require mcuelenaere/plitz
Usage
There are 2 ways to use the functionality provided by Plitz:
Blitz compatibility layer
$template = <<<EOF Hello {{ audience }}! EOF ; $assignments = [ 'audience' => 'world' ]; // construct Blitz object $blitz = new Plitz\Bindings\Blitz\Blitz(); // load template $blitz->load($template); // render template to stdout $blitz->display($assignments);
Direct access to Plitz classes
$template = <<<EOF Hello {{ audience }}! EOF ; $assignments = [ 'audience' => 'world' ]; // wrap template in a simple data:// stream $inputStream = fopen("data://text/plain;base64," . base64_encode($template), "r"); // write compiled template to a memory buffer $outputStream = fopen("php://memory", "r+"); try { // setup the required infrastructure $lexer = new Plitz\Lexer\Lexer($inputStream, "no template filename available"); $compiler = new Plitz\Compilers\PhpCompiler($outputStream); // or perhaps you want the Plitz\Compilers\JsCompiler ? $parser = new Plitz\Parser\Parser($lexer->lex(), $compiler); // lex and parse from the input stream and compile to the output stream $parser->parse(); // retrieve the compiled code from the memory stream fseek($outputStream, 0, SEEK_SET); $compiledCode = stream_get_contents($outputStream); } catch (Plitz\Lexer\LexException $ex) { printf("We got an exception from the lexer: %s (%s: line %d, pos %d)", $ex->getMessage(), $ex->getTemplateName(), $ex->getTemplateLine(), $ex->getTemplateColumn()); exit(1); } catch (Plitz\Parser\ParseException $ex) { printf("We got an exception from the parser: %s (%s: line %d, pos %d)", $ex->getMessage(), $ex->getTemplateName(), $ex->getTemplateLine(), $ex->getTemplateColumn()); exit(1); } finally { // cleanup when we're done fclose($inputStream); fclose($outputStream); } // create a function from the compiled code $templateFunction = create_function('$context', 'ob_start(); ?>' . $compiledCode . '<?php return ob_get_clean();'); // and last but not least: actually run it! echo $templateFunction($assignments);
Design
Plitz consists of 4 components:
- the lexer: tokenizes the input stream into a stream of
Plitz\Lexer\Tokens
- the parser: parses a
Plitz\Lexer\TokenStream
and informs aPlitz\Parser\Visitor
of the parsed blocks - the compiler: implements the
Plitz\Parser\Visitor
class and writes code to an output stream - the Blitz compatibility layer: wraps all the parts above up in a single class while trying to maintain source-compatibility with
Blitz