百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
M

Mu

> 编程语言
开源

Node.js 必须的 Mustache 模板引擎(和编译器)

743 stars0 点赞1 次浏览
访问官网GitHub

工具介绍

Node.js 必须的 Mustache 模板引擎(和编译器)

Mu - 一款快速、流式的 Node.js Mustache 引擎

警告: 此版本与 0.1 不兼容。

安装

我遇到了一些 npm 身份验证问题, 并且陷入了困境, 因此为了 现在你必须: npm install mu2

问题

目前,mu 不支持更改标签格式 ({{ }} 到 say)。

使用方法

有几种方法可以使用 mu 0.5。以下是最简单的一种方法:

javascript
var mu = require('mu2'); // notice the "2" which matches the npm repo, sorry..

mu.root = __dirname + '/templates'
mu.compileAndRender('index.html', {name: "john"})
  .on('data', function (data) {
    console.log(data.toString());
  });

以下是将其与 http 模块混合的示例:

javascript
var http = require('http')
  , util = require('util')
  , mu   = require('mu2');

mu.root = __dirname + '/templates';

  http.createServer(function (req, res) {

  var stream = mu.compileAndRender('index.html', {name: "john"});
  stream.pipe(res);

}).listen(8000);

在这里使用最后一个示例是一个小技巧,可用于始终编译模板 处于开发模式(因此更改会立即反映出来)。

javascript
var http = require('http')
  , util = require('util')
  , mu   = require('mu2');

mu.root = __dirname + '/templates';

http.createServer(function (req, res) {

  if (process.env.NODE_ENV == 'DEVELOPMENT') {
    mu.clearCache();
  }

  var stream = mu.compileAndRender('index.html', {name: "john"});
  util.pump(stream, res);

}).listen(8000);

API

mu.root

查找模板的路径。默认为工作目录。

此函数用于编译模板。通常您不会直接使用它,但在做一些奇怪的事情时,它可能会对您有用。

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

JavaScript

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月17日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言