xp-forge / handlebars
Handlebars for XP Framework
Installs: 31 529
Dependents: 1
Suggesters: 0
Security: 0
Stars: 0
Watchers: 3
Forks: 0
Open Issues: 1
Requires
- php: >=7.0.0
- xp-forge/mustache: ^8.2
- xp-framework/core: ^12.0 | ^11.0 | ^10.0
- xp-framework/logging: ^11.0 | ^10.0 | ^9.1
Requires (Dev)
- xp-framework/test: ^2.0 | ^1.0
- dev-master
- v9.4.0
- v9.3.0
- v9.2.0
- v9.1.0
- v9.0.0
- v8.1.1
- v8.1.0
- v8.0.0
- v7.1.1
- v7.1.0
- v7.0.1
- v7.0.0
- v6.2.0
- v6.1.1
- v6.1.0
- v6.0.1
- v6.0.0
- v5.3.0
- v5.2.1
- v5.2.0
- v5.1.1
- v5.1.0
- v5.0.0
- v4.3.4
- v4.3.3
- v4.3.2
- v4.3.1
- v4.3.0
- v4.2.3
- v4.2.2
- v4.2.1
- v4.2.0
- v4.1.0
- v4.0.2
- v4.0.1
- v4.0.0
- v3.0.2
- v3.0.1
- v3.0.0
- v2.0.0
- v1.0.2
- v1.0.1
- v1.0.0
- v0.5.0
- v0.4.1
- v0.4.0
- v0.3.0
- v0.2.0
- v0.1.0
- dev-feature/raise-errors-when-redeclaring
- dev-feature/streaming
- dev-refactor/composition-insteadof-inheritance
This package is auto-updated.
Last update: 2024-11-24 15:55:47 UTC
README
The Handlebars template language implemented for the XP Framework.
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:
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
{{#if licence}} A licence is available {{/if}} {{#if licence}} A licence is available {{else}} <em>Warning: No licence is available!</em> {{/if}} {{#if content}} Content {{else if hub}} Hub {{else}} Default {{/if}}
The "unless" block
{{#unless licence}} <em>Warning: No licence is available!</em> {{/unless}}
The "with" block
{{#with person}} Full name: {{firstName}} {{lastName}} {{/with}}
The "each" block
<ul> {{#each students}} <li>Student's name: {{firstName}} {{lastName}}</li> {{/each}} </ul>
All of the above block helpers support the else
statement.
The "log" helper
{{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:
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:
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)