phly-mustache is a Mustache implementation written for PHP

Overview

phly-mustache

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 {{ 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:

first_name . ' ' . $this->last_name; } }">
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.

full_name = function() use ($view) { return $view->first_name . ' ' . $view->last_name; };">
$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.
You might also like...
A GETTR.com client library written in PHP with Laravel support.
A GETTR.com client library written in PHP with Laravel support.

Gettr API Clinet PHP A GETTR.com client library written in PHP with Laravel support. This library uses unofficial publicly accessible API endpoints of

NFC Reader with the libnfc written in PHP
NFC Reader with the libnfc written in PHP

What is libnfc-for-php? This library is a toy for me. You can read NFC with libnfc written in PHP. Requirements PHP 7.4 or later PHP FFI libnfc Suppor

NFC Reader written in PHP
NFC Reader written in PHP

What is nfc-for-php? This library is a toy for me. You can read NFC with libnfc written in PHP. Requirements PHP 7.4+ PHP FFI libnfc 1.8.0 (if you use

A console noughts and crosses game written in php

Tic-tac-toe A console noughts and crosses game written in php To play, simply clone the file Navigate to the file directory on your terminal and run t

This is a plugin written in PHP programming language and running on the PocketMine platform that works stably on the API 3.25.0 platform

This is a plugin written in PHP programming language and running on the PocketMine platform that works stably on the API 3.25.0 platform. It allows you to hear the sound

Backend repository of libreoupas project, fully written in PHP, using the datas provided by the University.

libreoupas-engine/fst-nancy Description libreoupas est un site Internet permettant aux étudiant de la Faculté des Strasbourg illkirsh d'avoir accès au

Matplotlib. Pyplot written in PHP. Graphs charts, scatter, plot, lines, custom graphs
Matplotlib. Pyplot written in PHP. Graphs charts, scatter, plot, lines, custom graphs

php-libplot. Written in PHP. Graphs charts, scatter, plot, lines, custom graphs Matplotlib. Pyplot written in PHP. Graphs charts, scatter, plot, lines

Minimalistic bookmark manager for your own server written in PHP. Bookmarks are stored in a sqlite database.

b - Bookmark manager b is a minimalistic bookmark manager for your own server. Written in PHP. Bookmarks are stored in a sqlite database. Features: fi

A forum software written in vanilla PHP with a MariaDB/MySQL database.

GloomyBB GloomyBB is a simple forum software written in vanilla PHP with a MariaDB/MySQL database. It is currently still in early development but is u

Comments
  • You've found something that doesn't exist

    You've found something that doesn't exist

    Hi @weierophinney ,

    All the doc pages rendered are giving 404 : http://phly-mustache.readthedocs.org/en/latest/usage.html

    There is no such google groups : https://groups.google.com/forum/#!forum/phly-mustache

    Also the change log points to old repo PR?

    opened by harikt 3
  • Support YAML front matter

    Support YAML front matter

    The github specification now allows/defines YAML front matter for mustache documents; this provides a way to create default values for the view.

    In all cases, front matter begins and ends with a line containing only ---. You can have a single front matter section, or multiple front matter sections.

    A single front matter section:


    names: [ {name: chris}, {name: mark}, {name: scott} ]

    {{#names}} Hi {{name}}! {{/names}} Multiple front matter sections:


    name: chris

    name: mark

    name: scott

    Hi {{name}}! Technically, this is a feature of the mustache command, and not necessarily required by implementations. However, when considering things like template inheritance, it could be a useful feature.

    enhancement 
    opened by weierophinney 1
Owner
phly
phly
A sampling profiler for PHP written in PHP, which reads information about running PHP VM from outside of the process.

Reli Reli is a sampling profiler (or a VM state inspector) written in PHP. It can read information about running PHP script from outside of the proces

null 272 Dec 22, 2022
A sampling profiler for PHP written in PHP, which reads information about running PHP VM from outside of the process.

Reli Reli is a sampling profiler (or a VM state inspector) written in PHP. It can read information about running PHP script from outside of the proces

null 258 Sep 15, 2022
A multithreaded application server for PHP, written in PHP.

appserver.io, a PHP application server This is the main repository for the appserver.io project. What is appserver.io appserver.io is a multithreaded

appserver.io 951 Dec 25, 2022
A status monitor for Elite Dangerous, written in PHP. Designed for 1080p screens in the four-panel-view in panel.php, and for 7 inch screens with a resolution of 1024x600 connected to a Raspberry Pi.

EDStatusPanel A status monitor for Elite Dangerous, written in PHP. Designed for 1080p screens in the four-panel-view in panel.php, and for 7 inch scr

marcus-s 24 Oct 4, 2022
PHP generics written in PHP

PHP generics written in PHP Require PHP >= 7.4 Composer (PSR-4 Autoload) Table of contents How it works Quick start Example Features Tests How it work

Anton Sukhachev 173 Dec 30, 2022
A PHP parser written in PHP

PHP Parser This is a PHP 5.2 to PHP 8.1 parser written in PHP. Its purpose is to simplify static code analysis and manipulation. Documentation for ver

Nikita Popov 15.9k Jan 8, 2023
A beautiful, fully open-source, tunneling service - written in pure PHP

Expose A completely open-source ngrok alternative - written in pure PHP. Documentation For installation instructions, in-depth usage and deployment de

Beyond Code 3.9k Jan 7, 2023
Websocket chat room written in PHP based on workerman.

基于workerman的GatewayWorker框架开发的一款高性能支持分布式部署的聊天室系统。

walkor 1.1k Jan 8, 2023
A bot written in PHP which attempts to link IRC with SQL database, allowing for integration between platforms

Valeyard IRC-SQL-GateWay A bot written in PHP which attempts to link IRC with SQL database, allowing for integration between platforms. This bot is mo

Valerie Pond 10 Oct 6, 2022
Wake PC is a super tiny password protected webapp for linux machines that sends WOL packets, written in PHP.

Wake PC Wake PC is a super tiny password protected webapp for linux machines that sends WOL packets, written in PHP. How to set up Quick setup You can

Dániel Szabó 45 Dec 30, 2022