For faster navigation, this Iframe is preloading the Wikiwand page for Mustache (template system).

Mustache (template system)

This article has multiple issues. Please help improve it or discuss these issues on the talk page. (Learn how and when to remove these template messages) This article relies excessively on references to primary sources. Please improve this article by adding secondary or tertiary sources. Find sources: "Mustache" template system – news · newspapers · books · scholar · JSTOR (July 2019) (Learn how and when to remove this message) The neutrality of this article is disputed. Relevant discussion may be found on the talk page. Please do not remove this message until conditions to do so are met. (July 2019) (Learn how and when to remove this message) (Learn how and when to remove this message)
Mustache
Original author(s)Justin Hileman, Bruno Sutic, Chris Wanstrath, Ricardo Mendes, Bruno Michel
Initial release2009; 15 years ago (2009)
Stable release
4.2.0 / March 28, 2021; 3 years ago (2021-03-28)
Repository
TypeWeb template system
LicenseMIT
Websitemustache.github.io

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 lists and anonymous functions (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.[1][2]

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,[3] 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 (preserved) 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.

The input data can be a class so that input data can be characterized as an model–view–controller (MVC) view. The Mustache template does nothing but reference methods in the (input data) view.[3] All the logic, decisions, and code is contained in this view, and all the markup (ex. output XML) is contained in the template. In an model–view–presenter (MVP) context: input data is from MVP-presenter, and the Mustache template is the MVP-view.

Examples

Data

JSON data is fed to Mustache templates, resulting in an output. Here is some example data:

{
    "name": "World",
    "greater than": ">"
}

Variables

A simple Mustache template such as the one below, combined with the above data, would output Hello World.

Hello ((name))

Mustache HTML escapes data by default. For example, the below would render as 2 > 1.

2 ((greater than)) 1

To turn off escaping, use a &. The below would render as 2 > 1.

2 ((&greater than)) 1

If statements and foreach loops

Below is a template with section tag. When x is a Boolean value, the section tag acts like an if conditional. When x is an array, it acts like a foreach loop.

((#x))
Some text
((/x))

The special variable ((.)) 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.

<form>
    ((>textBox))
    ((>submitButton))
</form>

Comments

Comments are indicated using an exclamation mark.

((!comment goes here))

Technical details

Syntax highlighting is available in Atom, Coda, Emacs,[4] TextMate, Vim and Visual Studio Code.[5]

Mustache template support is built into many web application frameworks (ex. CakePHP)[citation needed]. 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 (see external links), that for final users results in the common syntax.

As of March 2011, the last SPEC_VERSION was 1.1.2.[6]

All Mustache Engines, in the v1.X architecture, have a render method, a Mustache_Compiler class and a Parser class.[citation needed]

Variations and derivatives

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

Handlebars

Handlebars.js[7] 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.[8]

Handlebars differs from its predecessor in that, within Block Expressions (similar to sections in Mustache), Helpers allow custom function through explicit user-written code for that block.

See also

References

  1. ^ Avola, G.; Raasch, J. (2012). Smashing Mobile Web Development. ISBN 9781118348123.
  2. ^ Cady, J. (2011). Functional Programming Applied to Web Development Templates: MS Project Report (PDF) (Report).
  3. ^ a b "Mustache". GitHub. 19 April 2022.
  4. ^ "Home". web-mode.org.
  5. ^ "Mustache - Visual Studio Marketplace". Visualstudio.com. Microsoft. August 18, 2019.
  6. ^ "Changes". Mustache. GitHub. March 20, 2011.
  7. ^ Katz, Yehuda (2011–2019). "Handlebars: Minimal templating on steroids". Handlebarsjs.com.
  8. ^ wykatz, NPM. "html+handlebars NPM". html+handlebars NPM package details. Node Package Manager. Retrieved 20 December 2016.
{{bottomLinkPreText}} {{bottomLinkText}}
Mustache (template system)
Listen to this article

This browser is not supported by Wikiwand :(
Wikiwand requires a browser with modern capabilities in order to provide you with the best reading experience.
Please download and use one of the following browsers:

This article was just edited, click to reload
This article has been deleted on Wikipedia (Why?)

Back to homepage

Please click Add in the dialog above
Please click Allow in the top-left corner,
then click Install Now in the dialog
Please click Open in the download dialog,
then click Install
Please click the "Downloads" icon in the Safari toolbar, open the first download in the list,
then click Install
{{::$root.activation.text}}

Install Wikiwand

Install on Chrome Install on Firefox
Don't forget to rate us

Tell your friends about Wikiwand!

Gmail Facebook Twitter Link

Enjoying Wikiwand?

Tell your friends and spread the love:
Share on Gmail Share on Facebook Share on Twitter Share on Buffer

Our magic isn't perfect

You can help our automatic cover photo selection by reporting an unsuitable photo.

This photo is visually disturbing This photo is not a good choice

Thank you for helping!


Your input will affect cover photo selection, along with input from other users.

X

Get ready for Wikiwand 2.0 🎉! the new version arrives on September 1st! Don't want to wait?