嗨,我想知道如何在dot.js模板引擎中呈现输出。我认为这是一个关于nodejs模板的一般性问题。(阅读评论了解更多信息)。我之所以选择这个模板引擎,而不是jade或ejs,是因为它看起来是最快的引擎。
这是我的app.js:
var express = require('express'),
app = express.createServer(),
doT = require('doT'),
pub = __dirname + '/public',
view = __dirname + '/views';
app.configure(function(){
app.set('views', view);
app.set('view options', {layout: false});
app.set('view engine', 'dot');
app.use(app.router);
});
app.register('.html', {
compile: function(str, opts){
return function(locals){
return str;
}
}
});
app.get('/', function(req, res){
//This is where I am trying to send data to the front end....
res.render('index.html', { output: 'someStuff' });
});下面是我的html:
<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8'>
<title>Index</title>
</head>
<body>
//This is where I am trying to receive data and output it...
{{=it.output}}
</body>
</html>我只是找不到关于它的好的文档。这还不够:http://olado.github.com/doT/。如果可以的话,请帮帮忙。这将极大地提高我对如何在nodejs中将数据传递到视图的理解。谢谢。
发布于 2012-02-12 14:20:26
您需要让express知道使用doT作为模板引擎,如下所示:
app.set("view engine", "html");
app.register('.html', doT);发布于 2013-02-10 09:27:42
我的帖子是一个无耻的插头,但它可能会帮助一些人。
我对现有模块在Express 3.x上的工作方式不太满意,我写了一个名为dot-emc的模块:
https://github.com/nerdo/dot-emc
用法与上面发布的用法类似。使用nom安装:
npm install dot-emc然后将其设置为默认的视图引擎。我更喜欢使用.def扩展,因为我的文本编辑器将.dot文件识别为Graphviz文件,所以语法略有不同:
app.engine("def", require("dot-emc").__express);
app.set("view engine", "def");然后,您可以开始使用它,就像在您的路由中使用任何其他视图引擎一样,例如:
app.get("/", function(req, res) {
res.render("index", {"title": "title goes here"});
});发布于 2012-11-11 05:11:11
如果你运行的是express 3,它还不受支持。但是,您可以使用express-dot:
npm install express-dot
然后在configure中
app.set('view engine', 'dot' );
app.engine('dot', require('express-dot').__express );然后在路由中:
res.render('profile', {}); // you will need to create views/profile.dothttps://stackoverflow.com/questions/9207078
复制相似问题