janl /mustache.js

Minimal templating with {{mustaches}} in JavaScript

★ 16,723⑂ 2,336👁 384⑃ 359● 490▤ 16
JavaScriptMITCreated 2009-10-04mustache.github.io
Download: v4.2.0v4.2.0 ↓4.2.02021-03-28
+0
Daily
16,723
stars
2,336
forks
384
watchers
359
prs
490
issues
16
releases

README

mustache.js - Logic-less {{mustache}} templates with JavaScript

 What could be more logical awesome than no logic at all?



mustache.js is a zero-dependency implementation of the mustache template system in JavaScript.

Mustache is a logic-less template syntax. It can be used for HTML, config files, source code - anything. It works by expanding tags in a template using values provided in a hash or object.

We call it "logic-less" because there are no if statements, else clauses, or for loops. Instead there are only tags. Some tags are replaced with a value, some nothing, and others a series of values.

For a language-agnostic overview of mustache's template syntax, see the mustache(5) manpage.

 Where to use mustache.js?

You can use mustache.js to render mustache templates anywhere you can use JavaScript. This includes web browsers, server-side environments such as Node.js, and CouchDB views.

mustache.js ships with support for the CommonJS module API, the Asynchronous Module Definition API (AMD) and ECMAScript modules.

In addition to being a package to be used programmatically, you can use it as a command line tool.

And this will be your templates after you use Mustache:

!'stache

 Install

You can get Mustache via npm.

bash
$ npm install mustache --save


 Usage

Below is a quick example how to use mustache.js:

js
const Mustache = require('mustache');

const view = {
  title: "Joe",
  calc: () = ( 2 + 4 )
};

const output = Mustache.render("{{title}} spends {{calc}}", view);


In this example, the Mustache.render function takes two parameters: 1) the mustache template and 2) a view object that contains the data and code needed to render the template.

 Templates

A mustache template is a string that contains any number of mustache tags. Tags are indicated by the double mustaches that surround them. {{person}} is a tag, as is {{person}}. In both examples we refer to person as the tag's key. There are several types of tags available in mustache.js, described below.

There are several techniques that can be used to load templates and hand them to mustache.js, here are two of them:

 Include Templates

If you need a template for a dynamic part in a static website, you can consider including the template in the static HTML file to avoid loading templates separately. Here's a small example:

js
// file: render.js

function renderHello() {
  const template = document.getElementById('template').innerHTML;
  const rendered = Mustache.render(template, { name: 'Luke' });
  document.getElementById('target').innerHTML = rendered;
}


html

  
    Loading...
    
      Hello {{ name }}!
    

    
    
  



 Load External Templates

If your templates reside in individual files, you can load them asynchronously and render them when they arrive. Another example using fetch:

js
function renderHello() {
  fetch('template.mustache')
    .then((response) = response.text())
    .then((template) = {
      const rendered = Mustache.render(template, { name: 

Top issues

Latest release

v4.2.0 v4.2.0 2021-03-28
773: Add package.json exports field, by @manzt.

Snapshot: 2026-11-16T09:23:14Z · Data license Apache-2.0 · JSON