janl / mustache.js
Minimal templating with {{mustaches}} in JavaScript
AI Architecture Analysis
This repository is indexed by RepoMind. By analyzing janl/mustache.js in our AI interface, you can instantly generate complete architecture diagrams, visualize control flows, and perform automated security audits across the entire codebase.
Our Agentic Context Augmented Generation (Agentic CAG) engine loads full source files into context on-demand, avoiding the fragmentation of traditional RAG systems. Ask questions about the architecture, dependencies, or specific features to see it in action.
Repository Overview (README excerpt)
Crawler viewmustache.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 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: Install You can get Mustache via npm. Usage Below is a quick example how to use mustache.js: In this example, the function takes two parameters: 1) the mustache template and 2) a 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. is a tag, as is . In both examples we refer to 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: 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: Variables The most basic tag type is a simple variable. A tag renders the value of the key in the current context. If there is no such key, nothing is rendered. All variables are HTML-escaped by default. If you want to render unescaped HTML, use the triple mustache: . You can also use to unescape a variable. If you'd like to change HTML-escaping behavior globally (for example, to template non-HTML formats), you can override Mustache's escape function. For example, to disable all escaping: . If you want _not_ to be interpreted as a mustache tag, but rather to appear exactly as in the output, you must change and then restore the default delimiter. See the Custom Delimiters section for more information. View: Template: Output: JavaScript's dot notation may be used to access keys that are properties of objects in a view. View: Template: Output: Sections Sections render blocks of text zero or more times, depending on the value of the key in the current context. A section begins with a pound and ends with a slash. That is, begins a section, while ends it. The text between the two tags is referred to as that section's "block". The behavior of the section is determined by the value of the key. False Values or Empty Lists If the key does not exist, or exists and has a value of , , , , or , or is an empty string or an empty list, the block will not be rendered. View: Template: Output: Non-Empty Lists If the key exists and is not , , or , and is not an empty list the block will be rendered one or more times. When the value is a list, the block is rendered once for each item in the list. The context of the block is set to the current item in the list for each iteration. In this way we can loop over collections. View: Template: Output: When looping over an array of strings, a can be used to refer to the current item in the list. View: Template: Output: If the value of a section variable is a function, it will be called in the context of the current item in the list on each iteration. View: Template: Output: Functions If the value of a section key is a function, it is called with the section's literal block of text, un-rendered, as its first argument. The second argument is a special rendering function that uses the current view as its view argument. It is called in the context of the current view object. View: Template: Output: Inverted Sections An inverted section opens with instead of . The block of an inverted section is rendered only if the value of that section's tag is , , , *falsy* or an empty list. View: Template: Output: Comments Comments begin with a bang and are ignored. The following template: Will render as follows: Comments may contain newlines. Partials Partials begin with a greater than sign, like {{> box}}. Partials are rendered at runtime (as opposed to compile time), so recursive partials are possible. Just avoid infinite loops. They also inherit the calling context. Whereas in ERB you may have this: Mustache requires only this: Why? Because the file will inherit the and variables from the calling context. In this way you may want to think of partials as includes, imports, template expansion, nested templates, or subtemplates, even though those aren't literally the case here. For example, this template and partial: base.mustache: Names {{#names}} {{> user}} {{/names}} user.mustache: {{name}} Can be thought of as a single, expanded template: In mustache.js an object of partials may be passed as the third argument to . The object should be keyed by the name of the partial, and its value should be the partial text. Custom Delimiters Custom delimiters can be used in place of and by setting the new values in JavaScript or in templates. Sett…