Mustache (template system)

Last updated
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
Type Web template system
License MIT
Website mustache.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]

Contents

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:

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 {{greaterthan}} 1

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

2 {{&greaterthan}} 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

Related Research Articles

<span class="mw-page-title-main">JavaScript</span> High-level programming language

JavaScript, often abbreviated as JS, is a programming language and core technology of the Web, alongside HTML and CSS. 99% of websites use JavaScript on the client side for webpage behavior.

In computer science, a preprocessor is a program that processes its input data to produce output that is used as input in another program. The output is said to be a preprocessed form of the input data, which is often used by some subsequent programs like compilers. The amount and kind of processing done depends on the nature of the preprocessor; some preprocessors are only capable of performing relatively simple textual substitutions and macro expansions, while others have the power of full-fledged programming languages.

Programming languages can be grouped by the number and types of paradigms supported.

The following table lists the various web template engines used in Web template systems and a brief rundown of their features.

JSDoc is a markup language used to annotate JavaScript source code files. Using comments containing JSDoc, programmers can add documentation describing the application programming interface of the code they're creating. This is then processed, by various tools, to produce documentation in accessible formats like HTML and Rich Text Format. The JSDoc specification is released under CC BY-SA 3.0, while its companion documentation generator and parser library is free software under the Apache License 2.0.

Svelte is a free and open-source component-based front-end software framework, and language created by Rich Harris and maintained by the Svelte core team members.

A single-page application (SPA) is a web application or website that interacts with the user by dynamically rewriting the current web page with new data from the web server, instead of the default method of loading entire new pages. The goal is faster transitions that make the website feel more like a native app.

<span class="mw-page-title-main">Google Closure Tools</span> JavaScript developer toolkit

Google Closure Tools is a set of tools to help developers build rich web applications with JavaScript. It was developed by Google for use in their web applications such as Gmail, Google Docs and Google Maps. As of 2023, the project had over 230K LOCs not counting the embedded Mozilla Rhino compiler.

<span class="mw-page-title-main">Node.js</span> JavaScript runtime environment

Node.js is a cross-platform, open-source JavaScript runtime environment that can run on Windows, Linux, Unix, macOS, and more. Node.js runs on the V8 JavaScript engine, and executes JavaScript code outside a web browser.

npm JavaScript package manager

npm is a package manager for the JavaScript programming language maintained by npm, Inc., a subsidiary of GitHub. npm is the default package manager for the JavaScript runtime environment Node.js and is included as a recommended feature in the Node.js installer.

AngularJS is a discontinued free and open-source JavaScript-based web framework for developing single-page applications. It was maintained mainly by Google and a community of individuals and corporations. It aimed to simplify both the development and the testing of such applications by providing a framework for client-side model–view–controller (MVC) and model–view–viewmodel (MVVM) architectures, along with components commonly used in web applications and progressive web applications.

Pretty Diff is a language-aware data comparison utility implemented in TypeScript. The online utility is capable of source code prettification, minification, and comparison of two pieces of input text. It operates by removing code comments from supported languages and then performs a pretty-print operation prior to executing the diff algorithm. An abbreviated list of unit tests is provided. The documentation claims the JavaScript pretty-print operation conforms to the requirements of JSLint.

<span class="mw-page-title-main">Elm (programming language)</span> Functional programming language

Elm is a domain-specific programming language for declaratively creating web browser-based graphical user interfaces. Elm is purely functional, and is developed with emphasis on usability, performance, and robustness. It advertises "no runtime exceptions in practice", made possible by the Elm compiler's static type checking.

JavaScript templating refers to the client side data binding method implemented with the JavaScript language. This approach became popular thanks to JavaScript's increased use, its increase in client processing capabilities, and the trend to outsource computations to the client's web browser. Popular JavaScript templating libraries are AngularJS, Backbone.js, Ember.js, Handlebars.js, JSX, Vue.js and Mustache.js. A frequent practice is to use double curly brackets to call values of the given key from data files, often JSON objects.

<span class="mw-page-title-main">Ember.js</span> JavaScript framework

Ember.js is an open-source JavaScript web framework that utilizes a component-service pattern. It is designed to allow developers to create scalable single-page web applications by incorporating common idioms, best practices, and patterns from other single-page-app ecosystem patterns into the framework.

Web Components are a set of features that provide a standard component model for the web allowing for encapsulation and interoperability of individual HTML elements. Web Components are a popular approach when building microfrontends.

<span class="mw-page-title-main">React (JavaScript library)</span> JavaScript library for building user interfaces

React is a free and open-source front-end JavaScript library for building user interfaces based on components by Facebook Inc. It is maintained by Meta and a community of individual developers and companies.

<span class="mw-page-title-main">Vue.js</span> Open-source JavaScript library for building user interfaces

Vue.js is an open-source model–view–viewmodel front end JavaScript framework for building user interfaces and single-page applications. It was created by Evan You and is maintained by him and the rest of the active core team members.

gulp is an open-source JavaScript toolkit, used as a streaming build system in front-end web development.

<span class="mw-page-title-main">Deno (software)</span> Secure JavaScript and TypeScript runtime

Deno is a runtime for JavaScript, TypeScript, and WebAssembly that is based on the V8 JavaScript engine and the Rust programming language. Deno was co-created by Ryan Dahl, who also created Node.js.

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. 1 2 "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.