xp-forge/handlebars
最新稳定版本:v10.0.1
Composer 安装命令:
composer require xp-forge/handlebars
包简介
Handlebars for XP Framework
README 文档
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; $engine= (new HandlebarsEngine())->withTemplates('src/main/handlebars'); $transformed= $engine->transform('hello', [ 'name' => 'World' ]);
Templates can also be declared inline:
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
{{#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('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.Nodeinstance - context: The current context, a
com.github.mustache.Contextinstance - options: The resolved options passed, in the above case the string "World" (which is what name resolves to)
Futher reading
统计信息
- 总下载量: 46.98k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 1
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: BSD-3-Clause
- 更新时间: 2015-01-10

