dev-master
9999999-dev https://github.com/thoom/phandlebarsHandlebars.js based templating engine for the Silex framework
MIT
The Requires
- php >=5.3.2
The Development Requires
by Z.d. Peacock
silex templates
Handlebars.js based templating engine for the Silex framework
Phandlebars allows you to use Handlebars templates in your Silex applications. Rather than reimplementing the Handlebars library in PHP, this provider uses server-side Javascript (primarily with node.js) to both compile the Handlebars templates you've created (making the generated JS file also available for your client-side handling) and process templates on the server., (*1)
There are two parts to the installation: the PHP hooks into your Silex application and the required server-side applications., (*2)
There are these options currently:, (*3)
HandlebarsServiceProvider::LIBRARY_FULL
.runtime.minify
command.uglifyjs
command.node
command.As an example:, (*4)
$app->register(new Thoom\Provider\HandlebarsServiceProvider(), array( 'handlebars.options' => array( 'debug' => $app['debug'], 'minify' => true, 'path.compiled.client' => $app['cache_dir'] . '/js/handlebars-compiled.js', 'path.compiled.server' => $app['cache_dir'] . '/compiled.js', 'path.templates.client' => __DIR__ . '/templates/client', 'path.templates.server' => __DIR__ . '/templates/server', ) ));
The provider needs a few server cli applications installed in order to render and compile templates., (*5)
Easy to install using the package manager of your choice. For instance, with Ubuntu:, (*6)
$ apt-get install nodejs build-essential
This assumes that your node executable is installed and accessible from node
. If you need to use a different command,
change the runtime.node
configuration option., (*7)
You'll need to install using NPM., (*8)
$ apt-get install npm $ npm install -g handlebars
If you want to minify your compiled templates, you'll need to install UglifyJS., (*9)
$ npm install -g uglify-js
If you prefer another minifier, note that it just needs to accept data from STDIN
and outputs to STDOUT
. To change the
minifier, change the runtime.minify
configuration option., (*10)
You may wish to precompile the script. This can be accomplished by calling the renderer from your application using the following command:, (*11)
$app['handlebars']->server();
To precompile handlebars for client only scripts:, (*12)
$app['handlebars']->client();
To render a server-side template for "index.handlebars":, (*13)
$app->get('/', function (Application $app) { return $app['handlebars']->render('index'); })->bind('homepage');
To add a global variable (like a user array) to be available to your templates:, (*14)
$app['handlebars']->addGlobal('user', $userArray);
To pass data to the template at render time, pass in a array as the second argument:, (*15)
$app['handlebars']->render('index', array('foo' => 'fez'));
To return an Http header other than 200, pass in a 3rd argument:, (*16)
$app['handlebars']->render('not-found', array('foo' => 'fez'), 404);
Note: Since the Handlebars templates are processed in Javascript using node, you can only pass items that are JSON serializable., (*17)
In addition to the standard Handlebars.js options, you have a few more options that allow you to extend templates similar to Twig., (*18)
To create a master template ('master.handlebars'), use the {{#block}} tag:, (*19)
<html> <head> <title>{{#block "title"}}Master Template{{/block}}</title> </head> ...
In your child template, you would use the {{#override}} and {{extend}} tags:, (*20)
{{#override "title"}}Child Template{{/override}} {{extend "master"}}
This would print:, (*21)
<html> <head> <title>Child Template</title> </head> ...
Notice that unlike Twig templates, the extend tag is found at the bottom of the script. This is required because of how Handlebars parses templates., (*22)
This convenience expression takes all of your Silex named routes and makes them available in your templates., (*23)
If your named route "login" has a path of "/login", you'd use:, (*24)
{{path "login" }}
If your named route "section" has a path of "/section/{section}", you'd use:, (*25)
{{path "section" section="my-section" }}
Handlebars.js based templating engine for the Silex framework
MIT
silex templates