2017 © Pedro Peláez
 

library handlebars

Ultra fast PHP7 implementation of handlebars.js

image

cradlephp/handlebars

Ultra fast PHP7 implementation of handlebars.js

  • Thursday, March 29, 2018
  • by cblanquera
  • Repository
  • 2 Watchers
  • 0 Stars
  • 190 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 4 Versions
  • 25 % Grown

The README.md

handlebars.php

Ultra fast PHP7 implementation of handlebars.js, (*1)

Travis CI Coverage Status Latest Stable Version Total Downloads Latest Unstable Version License, (*2)

Install

$ composer install cradlephp/handlebars

, (*3)

Introduction

PHP Handlebars and JS interface to match with 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., (*4)

, (*5)

Basic Usage

Rendering

use Cradle\Handlebars\HandlebarsHandler as Handlebars;
$handlebars = new Handlebars();
$template = $handlebars->compile('{{foo}} {{bar}}');

echo $template(['foo' => 'BAR', 'bar' => 'ZOO']);

Registering Helpers

$handlebars->registerHelper('bar', function($options) {
    return 'ZOO';
});

$template = $handlebars->compile('{{foo}} {{bar}}');

echo $template(['foo' => 'BAR']);

Registering Partials

$handlebars->registerPartial('bar', 'zoo');
$template = $handlebars->compile('{{foo}} {{> bar}}');

echo $template(['foo' => 'BAR']);

, (*6)

Features

  • 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|}}
      • Please note that there is an issue with each being slow depending on the size of the object
      • We need help optimizing this
    • with
    • unless
    • if

, (*7)

De-Features (or whatever the opposite of features is)

  • Does not support file templates.
    • You need to load them up and pass it into Handlebars.
    • If this is a problem you should consider other Handlebars PHP libraries
    • You can always create a helper for this
    • This de-feature will be considered upon requests ( create an issue :) )
  • Partial Failover
    • Something we haven't had a chance to come around doing yet as we did not have a need
    • This de-feature will be considered upon requests ( create an issue :) )
  • Safe String/Escaping
    • PHP has functions that can turn a string "safe".
    • We didn't want to create something that already exists in other contexts
    • This de-feature will be considered upon requests ( create an issue :) )
  • Utils
    • PHP has functions that support most of the listed Utils in handlebars.js
    • We didn't want to create something that already exists in other contexts
    • This de-feature will be considered upon requests ( create an issue :) )
  • Dynamic Partials
    • At the bottom of our pipe
    • because of it's difficulty to recreate
    • and practicality
    • This de-feature will be considered upon requests ( create an issue :( )
  • Inline Partials
    • TODO
  • Decorators
    • TODO
  • Frames
    • TODO

, (*8)

Production Ready

When your templates are ready for a production (live) environment, it is recommended that caching be used. To enable cache:, (*9)

  • 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.

, (*10)

API

, (*11)

compile

Returns a callback that binds the data with the template, (*12)

Usage

$handlebars->compile(string $string);

Parameters

  • string $string - the template string

Returns function - the template binding handler, (*13)

Example

$handlebars->compile();

, (*14)

getCache

Returns the active cache path, (*15)

Usage

$handlebars->getCache();

Returns Closure, (*16)


, (*17)

getHelper

Returns a helper given the name, (*18)

Usage

$handlebars->getHelper('if');

Parameters

  • string $name - the name of the helper

Returns Closure, (*19)


, (*20)

getHelpers

Returns all the registered helpers, (*21)

Usage

$handlebars->getHelpers();

Parameters

Returns array, (*22)


, (*23)

getPartial

Returns a partial given the name, (*24)

Usage

$handlebars->getPartial('foobar');

Parameters

  • string $name - the name of the partial

Returns string, (*25)


, (*26)

getPartials

Returns all the registered partials, (*27)

Usage

$handlebars->getPartials();

Parameters

Returns array, (*28)


, (*29)

registerHelper

The famous register helper matching the Handlebars API, (*30)

Usage

$handlebars->registerHelper(string $name, function $helper);

Parameters

  • string $name - the name of the helper
  • function $helper - the helper handler

Returns Eden\Handlebrs\Index, (*31)

Example

$handlebars->registerHelper();

, (*32)

registerPartial

Delays registering partials to the engine because there is no add partial method..., (*33)

Usage

$handlebars->registerPartial(string $name, string $partial);

Parameters

  • string $name - the name of the helper
  • string $partial - the helper handler

Returns Eden\Handlebrs\Index, (*34)

Example

$handlebars->registerPartial();

, (*35)

setCache

Enables the cache option, (*36)

Usage

$handlebars->setCache(string $path);

Parameters

  • string $path - The cache path

Returns Eden\Handlebrs\Index, (*37)

Example

$handlebars->setCache('/path/to/cache/folder');

, (*38)

setPrefix

Sets the file name prefix for caching, (*39)

Usage

$handlebars->setPrefix(string $prefix);

Parameters

  • string $prefix - Custom prefix name

Returns Eden\Handlebrs\Index, (*40)

Example

$handlebars->setPrefix('special-template-');

, (*41)

unregisterHelper

The opposite of registerHelper, (*42)

Usage

$handlebars->unregisterHelper(string $name);

Parameters

  • string $name - the helper name

Returns Eden\Handlebars\Index, (*43)

Example

$handlebars->unregisterHelper();

, (*44)

unregisterPartial

The opposite of registerPartial, (*45)

Usage

$handlebars->unregisterPartial(string $name);

Parameters

  • string $name - the partial name

Returns Eden\Handlebars\Index, (*46)

Example

$handlebars->unregisterPartial();

The Versions

29/03 2018

dev-master

9999999-dev https://cradlephp.github.io/

Ultra fast PHP7 implementation of handlebars.js

  Sources   Download

MIT

The Requires

 

The Development Requires

by Christian Blanquera

handlebars cradle cradlephp

29/03 2018

1.0.2

1.0.2.0 https://cradlephp.github.io/

Ultra fast PHP7 implementation of handlebars.js

  Sources   Download

MIT

The Requires

 

The Development Requires

by Christian Blanquera

handlebars cradle cradlephp

16/03 2018

1.0.1

1.0.1.0 https://cradlephp.github.io/

Ultra fast PHP7 implementation of handlebars.js

  Sources   Download

MIT

The Requires

 

The Development Requires

by Christian Blanquera

handlebars cradle cradlephp

11/03 2018

1.0.0

1.0.0.0 https://cradlephp.github.io/

Ultra fast PHP7 implementation of handlebars.js

  Sources   Download

MIT

The Requires

 

The Development Requires

by Christian Blanquera

handlebars cradle cradlephp