https://github.com/xp-forge/handlebars
Handlebars for XP Framework
https://github.com/xp-forge/handlebars
handlebars php templates xp-framework
Last synced: about 1 year ago
JSON representation
Handlebars for XP Framework
- Host: GitHub
- URL: https://github.com/xp-forge/handlebars
- Owner: xp-forge
- Created: 2013-12-30T10:42:58.000Z (over 12 years ago)
- Default Branch: master
- Last Pushed: 2025-05-04T16:34:49.000Z (about 1 year ago)
- Last Synced: 2025-05-04T17:02:40.732Z (about 1 year ago)
- Topics: handlebars, php, templates, xp-framework
- Language: PHP
- Size: 327 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Changelog: ChangeLog.md
Awesome Lists containing this project
README
Handlebars for XP Framework
============================
[](https://github.com/xp-forge/handlebars/actions)
[](https://github.com/xp-framework/core)
[](https://github.com/xp-framework/core/blob/master/LICENCE.md)
[](http://php.net/)
[](http://php.net/)
[](https://packagist.org/packages/xp-forge/handlebars)
The [Handlebars template language](http://handlebarsjs.com/) implemented for the XP Framework.
```php
use com\handlebarsjs\HandlebarsEngine;
$engine= new HandlebarsEngine();
$transformed= $engine->render('Hello {{name}}', [
'name' => 'World'
]);
```
Templating
----------
Templates can be loaded from the file system. The following loads and transforms the template *src/main/handlebars.handlebars*:
```php
use com\handlebarsjs\HandlebarsEngine;
$engine= (new HandlebarsEngine())->withTemplates('src/main/handlebars');
$transformed= $engine->transform('hello', [
'name' => 'World'
]);
```
Templates can also be declared inline:
```php
use com\handlebarsjs\HandlebarsEngine;
$engine= (new HandlebarsEngine())->withTemplates(['test' => 'Hello {{name}}']);
$transformed= $engine->transform('hello', [
'name' => 'World'
]);
```
If you need more flexibility, you can implement and then pass instances of the following:
* `new com.github.mustache.InMemory([:string] $templates))`
* `new com.github.mustache.FilesIn(string|io.Folder $arg, string[] $extensions)`
* `new com.github.mustache.ResourcesIn(string|lang.IClassLoader $arg, string[] $extensions)`
* (Your own implementation of `com.github.mustache.templates.Templates`)
Helpers supported
-----------------
The following helpers are built in:
### The "if" block
```handlebars
{{#if licence}}
A licence is available
{{/if}}
{{#if licence}}
A licence is available
{{else}}
Warning: No licence is available!
{{/if}}
{{#if content}}
Content
{{else if hub}}
Hub
{{else}}
Default
{{/if}}
```
### The "unless" block
```handlebars
{{#unless licence}}
Warning: No licence is available!
{{/unless}}
```
### The "with" block
```handlebars
{{#with person}}
Full name: {{firstName}} {{lastName}}
{{/with}}
```
### The "each" block
```handlebars
- Student's name: {{firstName}} {{lastName}}
{{#each students}}
{{/each}}
```
All of the above block helpers support the `else` statement.
### The "log" helper
```handlebars
{{log '"Hello", Frank\'s mother said.'}}
{{log 'No publishers for' category level="warn"}}
```
To enable logging, pass either a closure or a `util.log.LogCategory` instance to the engine:
```php
use util\log\Logging;
use util\cmd\Console;
// Use a logger category:
$logger= Logging::named('trace')->toConsole();
// Or a closure:
$logger= function($args, $level) { Console::writeLine('[', $level, '] ', ...$args); };
$engine= (new HandlebarsEngine())->withLogger($logger);
$engine->render(...);
```
Custom helpers
--------------
To add custom helpers, use *withHelpers()* and pass functions. The following yields *Hello WORLD*:
```php
use com\handlebarsjs\HandlebarsEngine;
$engine= (new HandlebarsEngine())->withHelper('upper', function($node, $context, $options) {
return strtoupper($options[0]);
});
$transformed= $engine->render('Hello {{upper name}}', [
'name' => 'World'
]);
```
The parameters passed are the following:
* **node:** The current node, a `com.github.mustache.Node` instance
* **context:** The current context, a `com.github.mustache.Context` instance
* **options:** The resolved options passed, in the above case the string "World" (which is what *name* resolves to)
Futher reading
--------------
https://handlebars-lang.github.io/spec/