如何在nodejs中渲染doT.js模板?
您好,我想知道如何在 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 中的视图的理解。谢谢。
Hi I would like to know how can I render output in dot.js templating engine. I think it's a generic question about nodejs templating.(read comments for more info). The reason why I chose this template engine instead of jade or ejs is because it seems the fastest engine around.
Here is my 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' });
});
Here is my 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>
I just could not find good docs on it. This was not enough: http://olado.github.com/doT/. Please help, if you can. This will improve my understanding exponentially of how data is passed to the view in nodejs. Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您需要让 Express 知道使用 doT 作为模板引擎,如下所示:
You need to let express know to use doT as the template engine like this:
我的帖子是一个无耻的插件,但它可能会帮助某人。
我对现有模块与 Express 3.x 的配合方式不太满意,我编写了一个名为 dot-emc 的模块:
https:// github.com/nerdo/dot-emc
用法与上面发布的类似。使用 nom: 安装它,
然后将其设置为默认视图引擎。我更喜欢使用 .def 扩展名,因为我的文本编辑器将 .dot 文件识别为 Graphviz 文件,因此语法略有不同:
然后您可以像在路线中使用任何其他视图引擎一样开始使用它,例如:
My post is a shameless plug, but it might help someone out.
I wasn't very happy with the way existing modules worked with Express 3.x, I wrote one called dot-emc:
https://github.com/nerdo/dot-emc
Usage is similar to what has been posted above. Install it with nom:
Then set it up as your default view engine. I prefer using the .def extension since my text editor recognizes .dot files as Graphviz files, so the syntax is slightly different:
Then you can start using it as you would any other view engine in your routes, e.g.:
如果您运行的是express 3,则尚不支持。但是,您可以使用express-dot:
npm installexpress-dot
然后在配置中
然后在路由中:
If you're running express 3, it's not supported yet. You can however use express-dot:
npm install express-dot
Then in configure
Then in routes:
我知道这是一个老问题,但我最近想使用标准生成的 Express 4.xx 应用程序来测试 doT。我没有找到 @olado 的明确示例来轻松与我生成的应用程序匹配。我尝试了不同的插件(让它工作,但不满意),所以我最终编写了这样的模板引擎,以便获得支持包含(#)的预编译点文件,而无需任何额外的插件:
现在我可以使用在像这样的路由中以标准的“res.render”方式(对于index.js):
记住在.dot模板文件中使用{{it.value}}。在上面的基本示例中,index.dot 看起来像这样:
I know this is an old question, but I recently wanted to test doT with a standard generated Express 4.x.x app. I did not find the express example from @olado to match easy with my generated app. I tried different plugins (getting it to work, but not satisfied), so I ended up writing the template engine like this in order to get pre-compiled dot files with support for includes(#) without any extra plugin:
Now I can use it in the standard "res.render" way in the routes like this (for index.js):
Remember to use {{it.value}} in the .dot template files. In basic example above, the index.dot would look something like this: