XaminProject/handlebars.php — pull request #33 by mardix (2013-11-27 06:35:09+00:00)
Title: 2.x php54

Hello world, I made some updates that I think the rest of the community could benefit from.

1. Requires PHP 5.4. > PHP 5.2 is out of the game and 5.3 is quickly following in the grave

2. Added {{else}} in {{#each}} . Else in each allow us to do something else if the Array is empty, instead of using {{#if}}

{{#each Array}}
something
{{else}}
Nothing found
{{/each}}

3. Added slice in each Array, to slice the data to display

{{#each Array[start:end]}}

Get the 10 items

{{#each array[0:10]}}
{{.}}
{{/each}}

Get the last two

{{#each array[-2:]}}
{{.}}
{{/each}}

more:

* {{#each Array[start:end]}} = starts at start trhough end -1
* {{#each Array[start:]}} = Starts at start though the rest of the array
* {{#each Array[:end]}} = Starts at the beginning through end -1
* {{#each Array[:]}} = A copy of the whole array
* {{#each Array[-1]}}
* {{#each Array[-2:]}} = Last two items
* {{#each Array[:-2]}} = Everything except last two items

4. Fix a bug in {{@key}} where if an array has one key/value element, it will not display @key

5. After create $handlebars, your can render by using it as function:

echo $handlebars("Hello {{you}}", ["you" => "me"]);

6. A better documentation

7. Change arrays from array to [] (php 5.4)

8. Cleanup