#Handlebars
Name: Handlebars
License: MIT
Version : 2.2.x
Requirements: PHP >= 5.4
Handlebars provides the power necessary to let you build semantic templates effectively with no frustration, that keep the view and the code separated like we all know they should be.
Fork of: Handlebars.php by XaminProject
Handlebars, is the PHP port of Handlebars.js
Extended docs can be found at VoodooPHP.org/docs/handlebars
You can just download Handlebars.php as is, or with Composer.
To install with composer, add the following in the require key in your composer.json file
"voodoophp/handlebars": "2.*"
composer.json
{
"name": "myapp/name",
"description": "My awesome app name",
"require": {
"voodoophp/handlebars": "2.*"
}
}
At the minimum, we are required to have an array model and a template string. Alternatively we can have a file containing handlebars (or html, text, etc) expression.
Handlebars templates look like regular HTML, with embedded handlebars expressions.
Handlebars HTML-escapes values returned by a {{expression}}.
<div class="entry">
<h1>{{title}}</h1>
<div class="body">
Hello, my name is {{name}}
</div>
</div>
The string above can be used as is in your PHP file, or be put in a file (ie: /templates/main.tpl), to be called upon rendering.
Now the we've created our template file, in a php file (index.php) we'll create the data to passed to the model. The model is a key/value array.
Below we are going to create the Handlebars object, set the partials loader, and put some data in the model.
/index.php
<?php
# With composer we can autoload the Handlebars package
require_once ("./vendor/autoload.php");
# If not using composer, you can still load it manually.
# require 'src/Handlebars/Autoloader.php';
# Handlebars\Autoloader::register();
use Handlebars\Handlebars;
use Handlebars\Loader\FilesystemLoader;
# Set the partials files
$partialsDir = __DIR__."/templates"
$partialsLoader = new FilesystemLoader($partialsDir,
[
"extension" => "html"
]
);
# We'll use $handlebars thoughout this the examples, assuming the will be all set this way
$handlebars = new Handlebars([
"loader" => $partialsLoader,
"partials_loader" => $partialsLoader
]);
# Will render the model to the templates/main.tpl template
echo $handlebars->render("main", $model);
The simplest way to assign data is to create an Array model. The model will contain all the data that will be passed to the template.
<?php
$model = [
"name" => "Mardix",
"title" => "I'm Title",
"permalink" => "blog/",
"foo" => "bar",
"article" => [
"title" => "My Article Title"
],
"posts" => [
[
"title" => "Post #1",
"id" => 1,
"content" => "Content"
],
[
"title" => "Post 2",
"id" => 2,
"content" => "Content"
]
]
];
Use the method Handlebars\Handlebars::render($template, $model)
to render you template once everything is created.
$template : Template can be the name of the file or a string containing the handlebars/html.
$model : Is the array that we will pass into the template
The code below will render the model to the templates/main.tpl template
echo $handlebars->render("main", $model);
Alternatively you can use $handlebars itself without invoking the render method
echo $handlebars("main", $model);
Let's use this simple model for the following examples, assuming everything is already set like above.
<?php
$model = [
"title" => "I'm Title",
"permalink" => "/blog/",
"foo" => "bar",
"article" => [
"title" => "My Article Title"
],
"posts" => [
[
"title" => "Post #1",
"id" => 1,
"content" => "Content"
],
[
"title" => "Post 2",
"id" => 2,
"content" => "Content"
]
]
];
Let's work with the template.
Handlebars expressions are the basic unit of a Handlebars template. You can use them alone in a {{mustache}}, pass them to a Handlebars helper, or use them as values in hash arguments.
The simplest Handlebars expression is a simple identifier:
{{title}}
-> I'm Title
Handlebars nested expressions which are dot-separated paths.
{{article.title}}
-> My Article Title
Handlebars nested expressions in an array.
{{posts.0.title}}
-> Post #1
Handlebars also allows for name conflict resolution between helpers and data fields via a this reference:
{{./name}} or {{this/name}} or {{this.name}}
Handlebars expressions with a helper. In this case we're using the upper helper
{{#upper title}}
-> I'M TITLE
Nested handlebars paths can also include ../ segments, which evaluate their paths against a parent context.
{{#each posts}}
<a href="/posts/{{../permalink}}/{{id}}">{{title}}</a>
{{content}}
{{/each}}
Handlebars HTML-escapes values returned by a {{expression}}. If you don't want Handlebars to escape a value, use the "triple-stash", {{{ }}}
{{{foo}}}
if/else
and unless
control structures are implemented as regular Handlebars helpers
You can use the if helper to conditionally render a block. If its argument returns false, null, "" or [] (a "falsy" value), Handlebars will not render the block.
Example
{{#if isActive}}
This part will be shown if it is active
{{else}}
This part will not show if isActive is true
{{/if}}
<?php
$model = [
"isActive" => true
];
echo $handlebars->render($template, $model)
You can use the unless helper as the inverse of the if helper. Its block will be rendered if the expression returns a falsy value.
{{#unless isActive}}
This part will not show if isActive is true
{{/unless}}