PHP code example of xp-forge / mustache

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


use com\github\mustache\MustacheEngine;

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

[
  'name'    => 'Willy',
  'wrapped' => function($text) {
    return '<b>'.$text.'</b>';
  }
];

[
  'name'    => 'Willy',
  'wrapped' => function($node, $context) {
    return '<b>'.strtoupper($node->evaluate($context)).'</b>';
  }
]

use com\github\mustache\{MustacheEngine, FilesIn};
use io\Folder;

$engine= new MustacheEngine();
$engine->withTemplates(new FilesIn(new Folder('templates')));
$transformed= $engine->transform('hello', ['name' => 'World']);

use com\github\mustache\MustacheEngine;

$engine= new MustacheEngine();
$template= $engine->compile($template);

// Later on:
$result1= $engine->evaluate($template, $variables1);
$result2= $engine->evaluate($template, $variables2);

use com\github\mustache\MustacheEngine;

$engine= new MustacheEngine();
$engine->withHelper('bold', function($text) {
  return '<b>'.$text.'</b>';
});
$transformed= $engine->render($template, ['location' => 'Spartaaaaa']);

// Declaration
class LocalizationHelpers {
  public function date($list, $context) {
    return $context->lookup($list->nodeAt(0)->name())->toString('d.m.Y');
  }

  public function money($list, $context) {
    // ...
  }
}

// Usage with engine instance
$engine->withHelper('local', new LocalizationHelpers());