Mustacheis aweb template system.It is described as alogic-lesssystem because it lacks any explicitcontrol flowstatements, likeif
andelse
conditionalsorfor loops;however, both looping and conditional evaluation can be achieved using section tags processinglistsandanonymous functions(lambdas). It is named "Mustache" because of heavy use ofbraces,{ }
,that resemble a sidewaysmoustache.Mustache is used mainly for mobile and web applications.[1][2]
Original author(s) | Justin Hileman, Bruno Sutic,Chris Wanstrath,Ricardo Mendes, Bruno Michel |
---|---|
Initial release | 2009 |
Stable release | 4.2.0
/ March 28, 2021 |
Repository | |
Type | Web template system |
License | MIT |
Website | mustache |
Implementationsare available inActionScript,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,andXQuery.
History and principles
editMustache-1was inspired by ctemplate and et,[3]and began as aGitHubdistribution at the end of 2009. A first version of thetemplate enginewas implemented withRuby,runningYAMLtemplate texts. The (preserved) main principles were:
- Logic-less:no explicit control flow statements, all controldriven by data.
- Strongseparation of concerns:logic from presentation:it is impossible to embed application logic in the templates.
The input data can be aclassso that input data can be characterized as amodel–view–controller(MVC) view. The Mustachetemplatedoes nothing but reference methods in the (input data)view.[3]All the logic, decisions, and code is contained in thisview,and all the markup (ex. outputXML) is contained in thetemplate.In amodel–view–presenter(MVP) context: input data is from MVP-presenter,and the Mustache template is the MVP-view.
Examples
editData
editJSONdata is fed to Mustache templates, resulting in an output. Here is some example data:
{
"name":"World",
"greater than":">"
}
Variables
editA simple Mustache template such as the one below, combined with the above data, would outputHello World
.
MustacheHTMLescapesdata by default. For example, the below would render as2 > 1
.
To turn off escaping, use a&
.The below would render as2 > 1
.
If statements and foreach loops
editBelow is a template with section tag. Whenx
is aBooleanvalue, the section tag acts like anifconditional.Whenx
is anarray,it acts like aforeach loop.
The special variable{{.}}
refers to the current item when looping through an array, or the item checked in a conditional.
Including other templates
edit
You can tell a Mustache template to load another Mustache template within it using the>
symbol.
Comments
editComments are indicated using an exclamation mark.
Technical details
editSyntax highlightingis available inAtom,Coda,Emacs,[4]TextMate,VimandVisual Studio Code.[5]
Mustache template support is built into manyweb application frameworks(ex.CakePHP)[citation needed].Support in JavaScript includes bothclient-sideprogramming with manyJavaScript librariesandAjax frameworkssuch asjQuery,DojoandYUI,as well asserver-side JavaScriptusingNode.jsandCommonJS.
Specification and implementations
editThere are manyMustache Engineimplementations available, and all of them meet a commonformal 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]
AllMustache Engines,in the v1.X architecture, have arendermethod, aMustache_Compilerclass and aParserclass.[citation needed]
Variations and derivatives
editMustache inspired numerous JavaScript template libraries which forked from the original simplicity to add certain functionality or use.[citation needed]
Handlebars
editHandlebars.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, withinBlock Expressions(similar to sections in Mustache),Helpersallow custom function through explicit user-written code for that block.
See also
editReferences
edit- ^Avola, G.; Raasch, J. (2012).Smashing Mobile Web Development.ISBN9781118348123.
- ^Cady, J. (2011).Functional Programming Applied to Web Development Templates: MS Project Report(PDF)(Report).
- ^ab"Mustache".GitHub.19 April 2022.
- ^"Home".web-mode.org.
- ^"Mustache - Visual Studio Marketplace".Visualstudio.com.Microsoft. August 18, 2019.
- ^"Changes".Mustache.GitHub. March 20, 2011.
- ^Katz, Yehuda (2011–2019)."Handlebars: Minimal templating on steroids".Handlebarsjs.com.
- ^wykatz, NPM."html+handlebars NPM".html+handlebars NPM package details.Node Package Manager.Retrieved20 December2016.