xp-forge/mustache
Composer 安装命令:
composer require xp-forge/mustache
包简介
The Mustache template language implemented for the XP Framework
README 文档
README
The mustache template language implemented for the XP Framework.
use com\github\mustache\MustacheEngine; $transformed= (new MustacheEngine())->render( 'Hello {{name}}', ['name' => 'World'] );
Introduction
Read the excellent mustache man-page for a start.
Features supported
This implementation supports all standard features of the current specification:
- Interpolation:
{{var}}({{&var}}and triple mustaches for unescaped) and the dot-notation. By default misses will be replaced by empty strings. - Comments:
{{! comment }}. Comments will appear in the parse tree but of course will be excluded when rendering. - Delimiters:
{{=@ @=}}will change the starting and ending delimiter to the@sign. Arbitrary length delimiters are supported. - Sections:
{{#section}}as well as inverted sections:{{^section}}are supported. - Partials:
{{> partial}}will load a template calledpartial.mustachefrom the template loader. - Implicit iterators: Written as
{{.}}, the current loop value will be selected.
The optional lambdas module as well as the parent context extension (../name) are also supported.
Lambdas
If the value is a closure, it will be invoked and the raw text (no interpolations will have been performed!) will be passed to it:
Template
{{# wrapped }} {{ name }} is awesome. {{/ wrapped }}
Data
[ 'name' => 'Willy', 'wrapped' => function($text) { return '<b>'.$text.'</b>'; } ];
Output
<b>Willy is awesome.</b>
Extended usage
The $text parameter passed is actually a com.github.mustache.Node instance but may be treated as text as it overloads the string cast. In order to work with it, the node's evaluate() method can be called with the com.github.mustache.Context instance given as the second argument:
[ 'name' => 'Willy', 'wrapped' => function($node, $context) { return '<b>'.strtoupper($node->evaluate($context)).'</b>'; } ]
Template loading
Per default, templates are loaded from the current working directory. This can be changed by passing a template loader instance to the engine:
use com\github\mustache\{MustacheEngine, FilesIn}; use io\Folder; $engine= new MustacheEngine(); $engine->withTemplates(new FilesIn(new Folder('templates'))); $transformed= $engine->transform('hello', ['name' => 'World']);
This will load the template stored in the file templates/hello.mustache. This template loader will also be used for partials.
Templates can also be loaded from the class loader, use the com.github.mustache.ResourcesIn and pass it a class loader instance (e.g. ClassLoader::getDefault() to search in all class paths) for this purpose.
Compiled templates
If you wish to apply variables to a template more than once, you can speed that process up by precompiling templates and using them later on:
use com\github\mustache\MustacheEngine; $engine= new MustacheEngine(); $template= $engine->compile($template); // Later on: $result1= $engine->evaluate($template, $variables1); $result2= $engine->evaluate($template, $variables2);
Helpers
Think of helpers as "omnipresent" context. They are added to the engine instance via withHelper() and will be available in any rendering context invoked on that instance.
Template
{{# bold }} This is {{ location }}! {{/ bold }}
Call
use com\github\mustache\MustacheEngine; $engine= new MustacheEngine(); $engine->withHelper('bold', function($text) { return '<b>'.$text.'</b>'; }); $transformed= $engine->render($template, ['location' => 'Spartaaaaa']);
Output
<b>This is Spartaaaaa!</b>
Using objects
You can also use instance methods as helpers, e.g.
// Declaration class LocalizationHelpers { public function date($list, $context) { return $context->lookup($list->nodeAt(0)->name())->toString('d.m.Y'); } public function money($list, $context) { // ... } } // Usage with engine instance $engine->withHelper('local', new LocalizationHelpers());
{{#local.date}}{{date}}{{/local.date}}
Spec compliance
Whether this implementation is compliant with the official spec can be tested as follows:
$ curl -sSL https://github.com/mustache/spec/archive/master.zip -o master.zip $ unzip master.zip && rm master.zip $ xp test com.github.mustache.** -a spec-master/specs/
xp-forge/mustache 适用场景与选型建议
xp-forge/mustache 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 50.9k 次下载、GitHub Stars 达 1, 最近一次更新时间为 2014 年 12 月 31 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「module」 「xp」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 xp-forge/mustache 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 xp-forge/mustache 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 xp-forge/mustache 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
bootstrap select field module implementing http://silviomoreto.github.io/bootstrap-select/
Yii2 module to manage website content. Kind of a CMS...
Codeurx Modular is simply a package for Laravel to help you create and manage modules.
User Module for the Attogram Framework
Info Module for the Attogram Framework
Contact Form Module for the Attogram Framework
统计信息
- 总下载量: 50.9k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 1
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: BSD-3-Clause
- 更新时间: 2014-12-31

