PHPackages                             mcuelenaere/plitz - 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. [Utility &amp; Helpers](/categories/utility)
4. /
5. mcuelenaere/plitz

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

mcuelenaere/plitz
=================

Pure PHP port of Blitz

0.0.4(10y ago)29.9k2[1 issues](https://github.com/mcuelenaere/plitz/issues)LGPL-2.1+PHPPHP &gt;=5.5

Since Feb 8Pushed 4y ago2 watchersCompare

[ Source](https://github.com/mcuelenaere/plitz)[ Packagist](https://packagist.org/packages/mcuelenaere/plitz)[ RSS](/packages/mcuelenaere-plitz/feed)WikiDiscussions master Synced 2w ago

READMEChangelog (4)Dependencies (4)Versions (9)Used By (0)

Plitz
-----

[](#plitz)

[![travis ci](https://camo.githubusercontent.com/e7e9909ab66df600604befd02aaec99e315ce2eace90d57e5f65c1fcd3334d59/68747470733a2f2f7472617669732d63692e6f72672f6d6375656c656e616572652f706c69747a2e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/mcuelenaere/plitz)[![coveralls](https://camo.githubusercontent.com/813f5f8a42801b95bf63549e8d302306d43c5a5e3747206f1da2acaf71635827/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f6d6375656c656e616572652f706c69747a2f62616467652e7376673f6272616e63683d6d6173746572)](https://coveralls.io/github/mcuelenaere/plitz)[![github tag](https://camo.githubusercontent.com/e05fcee65c5b4b16ecd213ffe8dfcb66cd3ce816fffd38b049011578bf5cce23/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f7461672f6d6375656c656e616572652f706c69747a2e737667)](https://github.com/mcuelenaere/plitz/tags)[![packagist downloads](https://camo.githubusercontent.com/b77133d29e5b49cef39936b1d9a384387c262c647b5d11a69c8c8413cc9f9239/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6d6375656c656e616572652f706c69747a2e737667)](https://packagist.org/packages/mcuelenaere/plitz)

Plitz is a pure PHP port of the [Blitz PHP template extension](https://github.com/alexeyrybak/blitz).

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

[](#installation)

Install with composer:

```
  composer require mcuelenaere/plitz
```

Usage
-----

[](#usage)

There are 2 ways to use the functionality provided by Plitz:

#### Blitz compatibility layer

[](#blitz-compatibility-layer)

```
$template =  '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

[](#direct-access-to-plitz-classes)

```
$template =  '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 . '
