Jump to content

Mustache (template system)

From Wikipedia, the free encyclopedia

This is an old revision of this page, as edited by HilaireFernandes (talk | contribs) at 19:58, 4 November 2015 (In the right alphabetical order). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

Mustache
Initial release2009 (2009)
Repository
LicenseMIT
Websitemustache.github.io Edit this on Wikidata

Mustache is a simple web template system with implementations available for ActionScript, C++, Clojure, CoffeeScript, ColdFusion, D, Delphi, Erlang, Fantom, Go, Haskell, Io, Java, JavaScript, Julia, Lua, .NET, Objective-C, Perl, PHP, Pharo, Python, Racket, Ruby, Rust, Scala, Swift, 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 curly braces ,{ }, that resemble a sideways moustache.

Mustache is used mainly for mobile and web applications.[1][2]

History and principles

Mustache-1 was inspired by ctemplate and et,[3] 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 (preserved) 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 (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 MVP context: input data is from MVP-presenter, and the Mustache template is the MVP-view.

Examples

The simplest template:

Hello {{name}}

Template with section tag:

{{#x}}
Some text
{{/x}}

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.

Technical details

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

The Mustache templates support is built into many web application frameworks (ex. CakePHP). The support in JavaScript includes both client-side programming with many popular JavaScript libraries and Ajax frameworks like 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 (see external links) — that for final users results in the common syntax. The last SPEC_VERSION is 1.1.2.

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

References

  1. ^ "Smashing Mobile Web Development", G. Avola and J. Raasch, 2012. ISBN 9781118348123.
  2. ^ "Functional Programming Applied to Web Development Templates", J. Cady, 2011. MS Project Report.
  3. ^ a b https://github.com/defunkt/mustache/blob/master/README.md
  4. ^ http://web-mode.org