-
Notifications
You must be signed in to change notification settings - Fork 12
7.2. Handlebars Template Engine
PHP Handlebars matches the JavaScript Version and has compile time helper support and super nice compile time error reporting. This version of Handlebars is based on caching the compiled templates and inherently made the overall compile times faster. Loading at ~50ms uncached and ~30ms cached.
use Cradle\Handlebars\HandlebarsHandler as Handlebars;
$handlebars = new Handlebars();
$template = $handlebars->compile('{{foo}} {{bar}}');
echo $template(['foo' => 'BAR', 'bar' => 'ZOO']);
$handlebars->registerHelper('bar', function($options) {
return 'ZOO';
});
$template = $handlebars->compile('{{foo}} {{bar}}');
echo $template(['foo' => 'BAR']);
$handlebars->registerPartial('bar', 'zoo');
$template = $handlebars->compile('{{foo}} {{> bar}}');
echo $template(['foo' => 'BAR']);
- PHP API - designed to match the handlebars.js documentation
- registerHelper() - Matches exactly what you expect from handlebars.js (except it's PHP syntax)
- registerPartial() - accepts strings and functions as callbacks
- Literals like
{{./foo}}
and{{../bar}}
are evaluated properly - Comments like
{{!-- Something --}}
and{{! Something }}
supported - Trims like
{{~#each}}
and{{~foo~}}
supported - Mustache backwards compatibility
{{#foo}}{{this}}{{/foo}}
- Tokenizer helpers to optimize custom code generation to cache
- Event handlers for unknown helpers and unknown partials
- Default Helpers matching handlebars.js
- each - and
{{#each foo as |value, key|}}
- with
- unless
- if
- each - and
When your templates are ready for a production (live) environment, it is recommended that caching be used. To enable cache:
- Create a cache folder and make sure permissions are properly set for handlebars to write files to it.
- Enable cache by using
$handlebars->setCache(__DIR__.'/your/cache/folder/location');
- If the folder location does not exist, caching will be disabled.
- compile
- getCache
- getHelper
- getHelpers
- getPartial
- getPartials
- registerHelper
- registerPartial
- setCache
- setPrefix
- unregisterHelper
- unregisterPartial
Returns a callback that binds the data with the template
$handlebars->compile(string $string);
-
string $string
- the template string
Returns function
- the template binding handler
$handlebars->compile();
Returns the active cache path
$handlebars->getCache();
Returns Closure
Returns a helper given the name
$handlebars->getHelper('if');
-
string $name
- the name of the helper
Returns Closure
Returns all the registered helpers
$handlebars->getHelpers();
Returns array
Returns a partial given the name
$handlebars->getPartial('foobar');
-
string $name
- the name of the partial
Returns string
Returns all the registered partials
$handlebars->getPartials();
Returns array
The famous register helper matching the Handlebars API
$handlebars->registerHelper(string $name, function $helper);
-
string $name
- the name of the helper -
function $helper
- the helper handler
Returns Eden\Handlebrs\Index
$handlebars->registerHelper();
Delays registering partials to the engine because there is no add partial method...
$handlebars->registerPartial(string $name, string $partial);
-
string $name
- the name of the helper -
string $partial
- the helper handler
Returns Eden\Handlebrs\Index
$handlebars->registerPartial();
Enables the cache option
$handlebars->setCache(string $path);
-
string $path
- The cache path
Returns Eden\Handlebrs\Index
$handlebars->setCache('/path/to/cache/folder');
Sets the file name prefix for caching
$handlebars->setPrefix(string $prefix);
-
string $prefix
- Custom prefix name
Returns Eden\Handlebrs\Index
$handlebars->setPrefix('special-template-');
The opposite of registerHelper
$handlebars->unregisterHelper(string $name);
-
string $name
- the helper name
Returns Eden\Handlebars\Index
$handlebars->unregisterHelper();
The opposite of registerPartial
$handlebars->unregisterPartial(string $name);
-
string $name
- the partial name
Returns Eden\Handlebars\Index
$handlebars->unregisterPartial();
2.B. Reference: Validation Types
2.D. Reference: Indexes & Relations
3.A. Reference: Cradle on Shared Hosts
3.B. Reference: Command Line Tools
3.C. Reference: Architecture Recommendations
4.4. Intro to Handlebars Templating
4.B. Reference: Handlebars Helpers
4.C. Reference: Doon Interfaces
4.D. Reference: Global Package Methods