定制 phly/phly-mustache 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

phly/phly-mustache

最新稳定版本:2.0.1

Composer 安装命令:

composer require phly/phly-mustache

包简介

PHP Mustache implementation supporting inheritance

关键字:

README 文档

README

⚠️ Archived 2025-08-17

Use at your own risk.

Build Status

phly-mustache is a Mustache implementation written for PHP. It conforms to the principles of mustache, and allows for extension of the format via pragmas.

In particular, it offers support for template inheritance ala hogan.js, using the {{<parent}} syntax.

For full documentation, please visit ReadTheDocs.

The mailing list is at https://groups.google.com/d/forum/phly_mustache

Installation

Install via composer:

$ composer require phly/phly-mustache

Documentation

Documentation builds are available at:

You can also build documentation in one of two ways:

  • MkDocs: Execute mkdocs build from the repository root.
  • Bookdown: Execute bookdown doc/bookdown.json from the repository root.

In each case, you can use PHP's built-in web server to serve the documentation:

$ php -S 0.0.0.0:8080 -t doc/html/

and then browse to http://localhost:8080/.

Usage

Basic usage is:

use Phly\Mustache\Mustache;

require 'vendor/autoload.php';

$mustache = new Mustache();
echo $mustache->render('some-template', $view);

By default, phly-mustache will look under the current directory for templates ending with '.mustache'; you can create a stack of directories using the default resolver:

use Phly\Mustache\Resolver\DefaultResolver;

$resolver = new DefaultResolver();
$resolver->addTemplatePath($path1);
$resolver->addTemplatePath($path2);

$resolver = $mustache->getResolver()->attach($defaultResolver);

In the above, it will search first $path2, then $path1 to resolve the template.

The default resolver is composed in an aggregate resolver by default; as such, you can also fetch it by type from the aggregate instead of adding it manually:

use Phly\Mustache\Resolver\DefaultResolver;

$resolver = $mustache->getResolver()->fetchByType(DefaultResolver::class);

Template names may be namespaced, using the syntax namespace::template:

$resolver->addTemplatePath($path1, 'blog');
$resolver->addTemplatePath($path2, 'contact');

Per the above configuratin, rendering the template contact::index will resolve to $path2. If it cannot, it will drop back to the default namespace (any paths registered without a namespace).

You may also change the suffix it will use to resolve templates:

$resolver->setSuffix('html'); // use '.html' as the suffix

If your templates use pragmas, you must first add pragma handlers to the Mustache pragma collection. This can be done as follows:

use Phly\Mustache\Pragma\ImplicitIterator as ImplicitIteratorPragma;

$mustache->getPragmas()->add(new ImplicitIteratorPragma());
$mustache->render('template-with-pragma', $view);

Views can be either associative arrays or objects. For objects, any public member, either a property or a method, may be referenced in your template. As an example:

class View
{
    public $first_name = 'Matthew';

    public $last_name  = "Weier O'Phinney";

    public function full_name()
    {
        return $this->first_name . ' ' . $this->last_name;
    }
}

Any property (or array key) may also refer to a valid callback; in such cases, the return value of the callback will be used.

$view = new stdClass;
$view->first_name = 'Matthew';
$view->last_name  = "Weier O'Phinney";
$view->full_name  = function() use ($view) {
    return $view->first_name . ' ' . $view->last_name;
};

Refer to the documentation (online / local) for full usage details.

Architecture

Phly\Mustache consists of five primary classes:

  • Lexer: tokenizes mustache syntax.
  • Renderer: renders a list of tokens, using substitions provided via a view.
  • Pragma\PragmaInterface: interface for pragmas, which may modify how tokens are handled.
  • Resolver\ResolverInterface: resolves a template name to mustache syntax or tokens.
  • Mustache: facade/gateway class. Tokenizes and renders templates, caches tokens, provides partial aliasing, aggregates pragmas, and acts as primary interface for end-users.

phly/phly-mustache 适用场景与选型建议

phly/phly-mustache 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 12.09k 次下载、GitHub Stars 达 15, 最近一次更新时间为 2015 年 09 月 02 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「mustache」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 phly/phly-mustache 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 phly/phly-mustache 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 12.09k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 15
  • 点击次数: 12
  • 依赖项目数: 2
  • 推荐数: 0

GitHub 信息

  • Stars: 15
  • Watchers: 3
  • Forks: 4
  • 开发语言: PHP

其他信息

  • 授权协议: BSD-2-Clause
  • 更新时间: 2015-09-02