Mustache (template system)


Mustache is a web template system with implementations available for ActionScript, C++, Clojure, CoffeeScript, ColdFusion, Common Lisp, D, Dart, Delphi, Erlang, Fantom, Go, Haskell, Io, Java, JavaScript, Julia, Lua,.NET, Objective-C, OCaml, Perl, PHP, Pharo, Python, R, Racket, Raku, Ruby, Rust, Scala, Smalltalk, Swift, Tcl, CFEngine and XQuery.
Mustache is described as a "logic-less" system because it lacks any explicit control flow statements, like if and else conditionals or for loops; however, both looping and conditional evaluation can be achieved using section tags processing lists and lambdas.
It is named "Mustache" because of heavy use of braces,, that resemble a sideways moustache.
Mustache is used mainly for mobile and web applications.

History and principles

Mustache-1 was inspired by ctemplate and et, and started as a GitHub distribution at the end of 2009. A first version of the template engine was implemented with Ruby, running YAML template texts. The main principles were:
The input data can be a class, so input data can be characterized as an MVC-view. The Mustache template does nothing but reference methods in the view. All the logic, decisions, and code is contained in this view, and all the markup is contained in the template. In an MVP context: input data is from MVP-presenter, and the Mustache template is the MVP-view.

Examples

The simplest template:

Hello

Template with section tag:

Some text

Here, when x is a Boolean value then the section tag acts like an if conditional, but when x is an array then it acts like a foreach loop.
Template that is not escaped:


Here, if body contains HTML, it won't be escaped.

Technical details

is available in Atom, Coda, Emacs, TextMate, Vim and Visual Studio Code.
The Mustache templates support is built into many web application frameworks. The support in JavaScript includes both client-side programming with many JavaScript libraries and Ajax frameworks such as jQuery, Dojo and YUI, as well as server-side JavaScript using Node.js and CommonJS.

Specification and implementations

There are many Mustache Engine implementations available, and all of them meet a common specification — that for final users results in the common syntax.
As of March 2011, the last SPEC_VERSION was 1.1.2.
All Mustache Engines, in the v1.X architecture, have a render method, a Mustache_Compiler class and a Parser class.

Variations and derivatives

Mustache inspired numerous JavaScript template libraries which forked from the original simplicity to add certain functionality or use.

Handlebars

is self-described as:
Handlebars.js is an extension to the Mustache templating language created by Chris Wanstrath. Handlebars.js and Mustache are both logicless templating languages that keep the view and the code separated like we all know they should be.

Handlebars differs from its predecessor in that, within "Block Expressions", "Helpers" allow custom functionality through explicit user-written code for that block.