Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/xp-forge/handlebars
Handlebars for XP Framework
https://github.com/xp-forge/handlebars
handlebars php templates xp-framework
Last synced: 8 days 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 (almost 11 years ago)
- Default Branch: master
- Last Pushed: 2024-03-24T14:29:40.000Z (8 months ago)
- Last Synced: 2024-04-25T19:22:38.734Z (7 months ago)
- Topics: handlebars, php, templates, xp-framework
- Language: PHP
- Size: 316 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Changelog: ChangeLog.md
Awesome Lists containing this project
README
Handlebars for XP Framework
============================[![Build status on GitHub](https://github.com/xp-forge/handlebars/workflows/Tests/badge.svg)](https://github.com/xp-forge/handlebars/actions)
[![XP Framework Module](https://raw.githubusercontent.com/xp-framework/web/master/static/xp-framework-badge.png)](https://github.com/xp-framework/core)
[![BSD Licence](https://raw.githubusercontent.com/xp-framework/web/master/static/licence-bsd.png)](https://github.com/xp-framework/core/blob/master/LICENCE.md)
[![Requires PHP 7.0+](https://raw.githubusercontent.com/xp-framework/web/master/static/php-7_0plus.svg)](http://php.net/)
[![Supports PHP 8.0+](https://raw.githubusercontent.com/xp-framework/web/master/static/php-8_0plus.svg)](http://php.net/)
[![Latest Stable Version](https://poser.pugx.org/xp-forge/handlebars/version.png)](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, FilesIn};$engine= (new HandlebarsEngine())->withTemplates(new FilesIn('src/main/handlebars'));
$transformed= $engine->transform('hello', [
'name' => 'World'
]);
```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(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/