2017 © Pedro Peláez
 

library mustache

The mustache template language implemented for the XP Framework

image

xp-forge/mustache

The mustache template language implemented for the XP Framework

  • Wednesday, March 14, 2018
  • by thekid
  • Repository
  • 3 Watchers
  • 1 Stars
  • 4,546 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 21 Versions
  • 11 % Grown

The README.md

Mustache

Build status on GitHub XP Framework Module BSD Licence Requires PHP 7.4+ Supports PHP 8.0+ Latest Stable Version, (*1)

The mustache template language implemented for the XP Framework., (*2)

use com\github\mustache\MustacheEngine;

$transformed= (new MustacheEngine())->render(
  'Hello {{name}}',
  ['name' => 'World']
);

Introduction

Read the excellent mustache man-page for a start., (*3)

Features supported

This implementation supports all standard features of the current specification:, (*4)

  • 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., (*5)

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:, (*6)

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:, (*7)

[
  '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:, (*8)

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., (*9)

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., (*10)

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:, (*11)

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., (*12)

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., (*13)

// 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:, (*14)

$ 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/

The Versions

14/03 2018
13/03 2018

dev-feature/streaming

dev-feature/streaming http://xp-framework.net/

The mustache template language implemented for the XP Framework

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

module xp

18/09 2017
17/09 2017
01/09 2017
03/06 2017
28/08 2016

v4.0.0

4.0.0.0 http://xp-framework.net/

The mustache template language implemented for the XP Framework

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

module xp

17/05 2016

v3.2.1

3.2.1.0 http://xp-framework.net/

The mustache template language implemented for the XP Framework

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

module xp

16/05 2016

v3.2.0

3.2.0.0 http://xp-framework.net/

The mustache template language implemented for the XP Framework

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

module xp

16/05 2016

v3.1.0

3.1.0.0 http://xp-framework.net/

The mustache template language implemented for the XP Framework

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

module xp

21/02 2016

v3.0.0

3.0.0.0 http://xp-framework.net/

The mustache template language implemented for the XP Framework

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

module xp

23/01 2016

v2.0.2

2.0.2.0 http://xp-framework.net/

The mustache template language implemented for the XP Framework

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

module xp

20/12 2015

v2.0.1

2.0.1.0 http://xp-framework.net/

The mustache template language implemented for the XP Framework

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

module xp

14/12 2015

v2.0.0

2.0.0.0 http://xp-framework.net/

The mustache template language implemented for the XP Framework

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

module xp

23/11 2015

v1.4.1

1.4.1.0 http://xp-framework.net/

The mustache template language implemented for the XP Framework

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

module xp

12/07 2015

v1.4.0

1.4.0.0 http://xp-framework.net/

The mustache template language implemented for the XP Framework

  Sources   Download

BSD-3-Clause

The Requires

 

module xp

07/04 2015

v1.3.4

1.3.4.0 http://xp-framework.net/

The mustache template language implemented for the XP Framework

  Sources   Download

BSD-3-Clause

The Requires

 

module xp

12/02 2015

v1.3.3

1.3.3.0 http://xp-framework.net/

The mustache template language implemented for the XP Framework

  Sources   Download

BSD-3-Clause

The Requires

 

module xp

30/01 2015

v1.3.2

1.3.2.0 http://xp-framework.net/

The mustache template language implemented for the XP Framework

  Sources   Download

BSD-3-Clause

The Requires

 

module xp

31/12 2014

v1.3.1

1.3.1.0 http://xp-framework.net/

The mustache template language implemented for the XP Framework

  Sources   Download

BSD-3-Clause

The Requires

 

module xp