Node.js 必须的 Mustache 模板引擎(和编译器)
警告: 此版本与 0.1 不兼容。
我遇到了一些 npm 身份验证问题, 并且陷入了困境, 因此为了 现在你必须: npm install mu2
目前,mu 不支持更改标签格式 ({{ }} 到 say)。
有几种方法可以使用 mu 0.5。以下是最简单的一种方法:
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 模块混合的示例:
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);在这里使用最后一个示例是一个小技巧,可用于始终编译模板 处于开发模式(因此更改会立即反映出来)。
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);mu.root
查找模板的路径。默认为工作目录。
此函数用于编译模板。通常您不会直接使用它,但在做一些奇怪的事情时,它可能会对您有用。
暂无开放 Issues,或尚未同步最近议题。