← 返回
未分类 中文

Mustache

Use mustache.js (logic-less Mustache templates) for any templating task in JavaScript/Node.js environments.
使用 mustache.js(无逻辑模板)在 JavaScript/Node.js 环境中完成所有模板任务。
openlark openlark 来源
未分类 clawhub v1.0.0 1 版本 100000 Key: 无需
★ 0
Stars
📥 298
下载
💾 0
安装
1
版本
#latest

概述

Mustache

Zero-dependency, logic-less template engine for JavaScript. Renders tags in templates using values from a view object.

Triggers

when the user asks to render templates, use Mustache syntax {{ }}, create .mustache files,generate HTML/config/code from templates, or mentions mustache/mustache.js/{{mustache}}.

Covers

variables, sections, inverted sections, partials, comments, custom delimiters, CLI usage,

pre-parsing/caching, and common patterns (email templates, config generation, code scaffolding).

Install

npm install mustache --save        # project dependency
npm install -g mustache            # CLI tool

Core API

const Mustache = require('mustache');
const html = Mustache.render(template, view, partials, tags);
  • template (string) — Mustache template string
  • view (object) — data & helper functions
  • partials (object, optional) — { name: partialString }
  • tags (string[], optional) — custom delimiters [ open, close ]

Tag Types Quick Reference

| Tag | Syntax | Behavior |

|-----|--------|----------|

| Variable | {{key}} | HTML-escaped value |

| Unescaped | {{{key}}} or {{&key}} | Raw HTML output |

| Dot notation | {{a.b.c}} | Nested property access |

| Current item | {{.}} | Current item in string array loop |

| Section | {{#key}}...{{/key}} | Truthy → render block; array → loop |

| Inverted | {{^key}}...{{/key}} | Falsy/empty → render block |

| Comment | {{! text }} | Stripped from output |

| Partial | {{> name}} | Inline another template |

| Set Delimiter | {{=<% %>=}} | Change tag delimiters |

Sections — Detailed

Falsy (skip): null, undefined, false, 0, NaN, "", [] → block not rendered.

Non-empty array (loop): Block renders once per item; context shifts to each item.

// String array — use {{.}} for current item
Mustache.render('{{#items}}- {{.}}\n{{/items}}', { items: ['a','b'] });
// → "- a\n- b\n"

// Object array — access properties directly
Mustache.render('{{#people}}* {{name}}\n{{/people}}', {
  people: [{ name: 'Alice' }, { name: 'Bob' }]
});
// → "* Alice\n* Bob\n"

// Lambda function — receives raw block text + render helper
Mustache.render('{{#bold}}Hi {{name}}{{/bold}}', {
  name: 'World',
  bold: function() {
    return function(text, render) {
      return '<b>' + render(text) + '</b>';
    };
  }
});
// → "<b>Hi World</b>"

Partials

Pass as third argument; inherit the calling context.

Mustache.render(
  '<h2>Names</h2>{{#names}}{{> user}}{{/names}}',
  { names: [{ name: 'Alice' }, { name: 'Bob' }] },
  { user: '<strong>{{name}}</strong>\n' }
);
// → <h2>Names</h2>\n<strong>Alice</strong>\n<strong>Bob</strong>\n

Custom Delimiters

// JS: pass as 4th argument or set Mustache.tags
Mustache.render(template, view, {}, ['<%', '%>']);

// Template: set delimiter inline
{{=<% %>=}}<% erb_style %><%={{ }}=%>
// Delimiters may not contain whitespace or =

Pre-parsing / Caching

Mustache.parse(template);  // cache parsed tree
// Later calls with the same template skip parsing

CLI Usage

mustache data.json template.mustache > output.html
mustache data.json -p partials/header.mustache -p partials/footer.mustache template.mustache
cat data.json | mustache - template.mustache > output.html

Common Patterns

For detailed examples and anti-patterns, see references/patterns.md.

Escape override (for non-HTML like config files):

Mustache.escape = t => t;

Include template in HTML (static sites):

<script id="tpl" type="x-tmpl-mustache">{{> content}}</script>

Async load template (SPAs):

fetch('tpl.mustache').then(r => r.text()).then(t => {
  document.getElementById('out').innerHTML = Mustache.render(t, data);
});

Anti-patterns

  • Do NOT put logic in views — Mustache is logic-less; move logic to pre-processing
  • Avoid recursive partials without a termination condition
  • Do NOT use Mustache.escape = t => t globally without restoring it afterward

版本历史

共 1 个版本

  • v1.0.0 当前
    2026-05-08 00:45 安全 安全

安全检测

腾讯云安全 (Keen)

安全,无风险
查看报告

腾讯云安全 (Sanbu)

安全,无风险
查看报告

🔗 相关推荐

dev-programming

Github

steipete
使用 `gh` CLI 与 GitHub 交互,通过 `gh issue`、`gh pr`、`gh run` 和 `gh api` 管理议题、PR、CI 运行及高级查询。
★ 678 📥 327,435
dev-programming

Mcporter

steipete
使用 mcporter CLI 直接列出、配置、认证及调用 MCP 服务器/工具(支持 HTTP 或 stdio),涵盖临时服务器、配置编辑及 CLI/类型生成功能。
★ 195 📥 67,660
dev-programming

CodeConductor.ai

larsonreever
AI驱动平台,提供快速全栈开发、智能体、工作流自动化及低代码AI集成的可扩展产品创建。
★ 72 📥 181,865