xp-forge/mustache 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

xp-forge/mustache

Composer 安装命令:

composer require xp-forge/mustache

包简介

The Mustache template language implemented for the XP Framework

关键字:

README 文档

README

Build status on GitHub XP Framework Module BSD Licence Requires PHP 7.4+ Supports PHP 8.0+ Latest Stable Version

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 called partial.mustache from 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 我们能提供哪些服务?
定制开发 / 二次开发

基于 xp-forge/mustache 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 50.9k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 1
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 2
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: BSD-3-Clause
  • 更新时间: 2014-12-31