Skip to content

Instantly share code, notes, and snippets.

@jeffturcotte
Created March 26, 2014 22:03
Show Gist options
  • Save jeffturcotte/9794620 to your computer and use it in GitHub Desktop.
Save jeffturcotte/9794620 to your computer and use it in GitHub Desktop.
Handlebars context helper concept
<?php
include 'vendor/autoload.php';
use Handlebars\Handlebars;
use Handlebars\Helpers;
use Handlebars\Loader\FilesystemLoader;
$helpers = new Helpers();
$helpers->add('data', function($template, $context, $args, $source) {
$context->push(['injected' => 'this var was injected']);
$buffer = $template->render($context);
$context->pop();
return $buffer;
});
$engine = new Handlebars(array(
'loader' => new FilesystemLoader(__DIR__),
'helpers' => $helpers
));
echo $engine->render('template', ['parent' => 'this is a parent var!']);
<div>
this var was injected
this is a parent var!
</div>
this is a parent var!
<div>
{{#data /totally/arbitrary/argument}}
{{injected}}
{{../parent}}
{{/data}}
</div>
{{parent}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment