Skip to content
Matthew Weier O'Phinney edited this page Sep 2, 2015 · 1 revision

phly-mustache is a Mustache implementation for PHP.

While the Mustache landing page links to a PHP implementation already, I found several things I wanted to improve upon:

  • Pragma handling requires extending the base Mustache class. This makes it difficult to mix-and-match pragmas.
  • Known issues with things like delimiter changes indicate potential brittleness of the lexer used.
  • Lack of caching of tokens; each time a template is rendered, it needs to be parsed. Considering partials are often used inside loops, this can lead to significant performance overhead.

My approach uses a fully-blown Lexer to ensure:

  • that is is scope-safe (e.g., ensuring pragma-specific syntax is only parsed and/or rendered if the pragma has been declared; ensuring partials are compiled in their own scope, etc.);
  • token caching is possible.

It also avoids a number of potential recursion issues when using partials.

The architecture is as follows:

  • Lexer: tokenizes a template.
  • Renderer: iterates token list and renders tokens using the view as context.
  • Pragma: provides an interface for pragmas, which are used by the Lexer and Renderer to extend the capabilities of the templating language.
  • Mustache: facade/gateway class. Tokenizes and renders templates, caches tokens, provides partials aliasing, and acts as primary interface for end users.

This approach provides a granular structure which makes configuration and extension simpler. Want to create a new pragma? Create a class, decide if/how it affects parsing and/or rendering, and attach it. Want automated caching? Write a facade to handle retrieving cached tokens, injecting them into the Mustache class, and retrieving and storing tokens when complete. Want to alter the tokenization process? Create pragmas, or extend the Lexer, and inject your implementation.

How can you help?

Read the contributing guide.

Clone this wiki locally