Mustache (template system)
Mustache is a web template system. It 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 List (abstract [data type)|lists] and anonymous functions. It is named "Mustache" because of heavy use of braces, , that resemble a sideways moustache. Mustache is used mainly for mobile and web applications.Implementations are available in ActionScript, C++, Clojure, CoffeeScript, ColdFusion, Common Lisp, Crystal, D, Dart, Delphi, Elixir, 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.
History and principles
Mustache-1 was inspired by ctemplate and et, and began 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:Logic-less: no explicit control flow statements, all control driven by data.- Strong separation of concerns: logic from presentation: it is impossible to embed application logic in the templates.
Examples
Data
JSON data is fed to Mustache templates, resulting in an output. Here is some example data:Variables
A simple Mustache template such as the one below, combined with the above data, would outputHello World.Hello
2 > 1.2 1
&. The below would render as 2 > 1.2 1
If statements and foreach loops
Below is a template with section tag. Whenx is a Boolean value, the section tag acts like an if conditional. When x is an array, it acts like a foreach loop.Some text
refers to the current item when looping through an array, or the item checked in a conditional.Including other templates
You can tell a Mustache template to load another Mustache template within it using the> symbol.Comments
Comments are indicated using an exclamation mark.Technical details
Syntax highlighting is available in Atom, Coda, Emacs, TextMate, Vim and Visual Studio Code.Mustache template support is built into many web application frameworks. 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 formal 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
Handlebars.js is self-described as:Handlebars differs from its predecessor in that, within Block Expressions, Helpers allow custom function through explicit user-written code for that block.