+0
日增长
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
#599Add option: Warning on unknown variables22 comments · 2016-09-14#399Accessing parent scope18 comments · 2014-12-11#797export 'render' (imported as 'render') was not found in 'mustache' (possible exports: default)14 comments · 2022-04-12#7864.2.0 broken (typescript + webpack):14 comments · 2021-06-10#277Registering global helpers 10 comments · 2012-12-05#437write an upgrade guide to avoid missing steps7 comments · 2015-03-24#825Feature request: {{<parent}} blocks5 comments · 2023-11-06#7754.2.0 breaks pre-14.x code5 comments · 2021-04-09
Latest release
v4.2.0 v4.2.0 2021-03-28773: Add package.json exports field, by @manzt.