[![PHPUnit](https://github.com/salesforce/handlebars-php/actions/workflows/ci.yml/badge.svg)](https://github.com/salesforce/handlebars-php/actions/workflows/ci.yml)
---
#handlebars-php
---
#### A simple, logic-less, yet powerful templating engine for PHP
---
Name: **handlebars-php**
License: MIT
Requirements: PHP >= 5.4
---
## About Handlebars
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](https://github.com/mardix/Handlebars)
Handlebars, is the PHP port of [Handlebars.js](http://handlebarsjs.com/)
---
## Install 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
`"salesforce/handlebars-php": "1.*"`
composer.json
```json
{
"name": "myapp/name",
"description": "My awesome app name",
"require": {
"salesforce/handlebars-php": "1.*"
}
}
```
-----
## Getting Started
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.
#### Template
Handlebars templates look like regular HTML, with embedded handlebars expressions.
Handlebars HTML-escapes values returned by a {{expression}}.
```html
{{title}}
Hello, my name is {{name}}
```
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.
#### PHP file
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
"html"
]
);
# We'll use $handlebars throughout 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
$model = [...];
echo $handlebars->render("main", $model);
```
#### Assign Data
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
"Yolo Baggins",
"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"
]
]
];
```
#### Render Template
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
```php
echo $handlebars->render("main", $model);
```
Alternatively you can use $handlebars itself without invoking the render method
```php
echo $handlebars("main", $model);
```
---
## Expressions
Let's use this simple model for the following examples, assuming everything is already set like above.
```php
"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:
```html
{{title}}
-> I'm Title
```
Handlebars nested expressions which are dot-separated paths.
```html
{{article.title}}
-> My Article Title
```
Handlebars nested expressions in an array.
```html
{{posts.0.title}}
-> Post #1
```
Handlebars also allows for name conflict resolution between helpers and data fields via a this reference:
```html
{{./name}} or {{this/name}} or {{this.name}}
```
Handlebars expressions with a helper. In this case we're using the upper helper
```html
{{#upper title}}
-> I'M TITLE
```
Nested handlebars paths can also include ../ segments, which evaluate their paths against a parent context.
```html
{{#each posts}}