Search code examples
javascriptnode.jsexpresstemplate-enginedot.js

How to render doT.js templating in 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.


Solution

  • You need to let express know to use doT as the template engine like this:

    app.set("view engine", "html");
    app.register('.html', doT);