PHP code example of mcuelenaere / plitz

1. Go to this page and download the library: Download mcuelenaere/plitz library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

mcuelenaere / plitz example snippets


$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);

$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 tream
  $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();