PHP code example of xp-forge / handlebars

1. Go to this page and download the library: Download xp-forge/handlebars 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/ */

    

xp-forge / handlebars example snippets


use com\handlebarsjs\HandlebarsEngine;

$engine= new HandlebarsEngine();
$transformed= $engine->render('Hello {{name}}', [
  'name' => 'World'
]);

use com\handlebarsjs\{HandlebarsEngine, FilesIn};

$engine= (new HandlebarsEngine())->withTemplates(new FilesIn('src/main/handlebars'));
$transformed= $engine->transform('hello', [
  'name' => 'World'
]);

use util\log\Logging;
use util\cmd\Console;

// Use a logger category:
$logger= Logging::named('trace')->toConsole();

// Or a closure:
$logger= function($args, $level) { Console::writeLine('[', $level, '] ', ...$args); };

$engine= (new HandlebarsEngine())->withLogger($logger);
$engine->render(...);

use com\handlebarsjs\HandlebarsEngine;

$engine= (new HandlebarsEngine())->withHelper(function($node, $context, $options) {
  return strtoupper($options[0]);
});
$transformed= $engine->render('Hello {{upper name}}', [
  'name' => 'World'
]);