如何在nodejs中渲染doT.js模板?

use*_*ser 5 javascript template-engine node.js express 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' });

});
Run Code Online (Sandbox Code Playgroud)

这是我的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>
Run Code Online (Sandbox Code Playgroud)

我就是找不到好的文档.这还不够:http://olado.github.com/doT/.如果可以的话请帮忙.这将提高我对数据如何传递到nodejs中的视图的指数级的​​理解.谢谢.

小智 14

你需要让表达知道使用doT作为模板引擎,如下所示:

app.set("view engine", "html");
app.register('.html', doT);
Run Code Online (Sandbox Code Playgroud)


cue*_*alk 5

我的帖子是一个无耻的插件,但它可能会帮助某人.

我对Express 3.x现有模块的使用方式不太满意,我写了一个名为dot-emc的文章:

https://github.com/nerdo/dot-emc

用法类似于上面发布的内容.使用nom安装它:

npm install dot-emc
Run Code Online (Sandbox Code Playgroud)

然后将其设置为默认视图引擎.我更喜欢使用.def扩展名,因为我的文本编辑器将.dot文件识别为Graphviz文件,因此语法略有不同:

app.engine("def", require("dot-emc").__express);
app.set("view engine", "def");
Run Code Online (Sandbox Code Playgroud)

然后,您可以像路径中的任何其他视图引擎一样开始使用它,例如:

app.get("/", function(req, res) {
    res.render("index", {"title": "title goes here"});
});
Run Code Online (Sandbox Code Playgroud)