node.js - 用于 Express 的 mustache (类)模板引擎?

标签 node.js express mustache

是否有基于 Mustache 或使用类似语法的 Express(node.js)模板引擎?

我能找到的只有 hamljadeejsjquery 模板 和一个基于 CoffeeScript 的模板 (我写纯 JS)。

我想写“普通”的 html,所以只有 ejsjqtpl 适合。我已经将 mustachebackbone 一起使用,因此最好也将它与 Node.js 在服务器端一起使用

最佳答案

只是偶然发现了这个古老的线程,但没有人提到 consolidate.js,这似乎是 Express 3 下的“正确”方式(请参阅 http://expressjs.com/faq.html)。它还提供了一种简单的方法来切换/试验模板系统。

这是一个简单的例子 - http://invitingthebell.com/2012/12/24/mustache-templates-in-express-3-0/ .

代码,以防它消失是:

var express = require('express')
 , cons = require('consolidate')
 , app = express();

// assign the mustache engine to .html files
app.engine('html', cons.mustache);

// set .html as the default extension 
app.set('view engine', 'html');

app.set('views', __dirname + '/views');

// test mustache
app.get('/', function(req, res){ 
  var viewdata = { 'test' : 'Hey now.'};
  res.render('index', viewdata);
});

app.listen(3000);

views目录下的index.html文件:

<html>
  <head><title>Some CMS</title></head>
  <body>
    <h1>Mustache</h1>
    <p>What do you say?</p>
    <p>{{test}}</p>
  </body>
</html>

关于node.js - 用于 Express 的 mustache (类)模板引擎?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8881574/

相关文章:

javascript - mustache 条件和循环

javascript - Jquery On() 'click' 处理每件事?

node.js - 将 mustache.js 与 node.js 一起使用?

node.js - n 或 nvm 用于管理 Node 版本 - 为每个版本保留全局模块是个好主意吗?

javascript - 在 Node.js 项目上运行客户端 javascript

javascript - 在 Node - Express 中执行 POST 操作后更改 EJS 中的元素样式

node.js - 如何在 localhost 上为通配符域创建一个 dns 服务器以与 node/express 一起使用?

javascript - 将 Nodejs 服务器转换为 Node 模块

node.js - 尝试使用假 JSON 数据,但无法在 HTML 页面中更新

javascript - NodeJS 中的 Axios.all 失败并出现 404 错误