How to make doT.js a template in nodejs?

Hi, I would like to know how I can display the output in a templating engine template. I think this is a general question about nodejs templates. (Read the comments for more information). The reason I chose this template engine instead of jade or ejs is because it seems like the fastest engine.

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 documents. This was not enough: http://olado.github.com/doT/ . Please help if you can. This will improve my understanding of exponentially how data is passed to view in nodejs. Thank.

+5
source share
4 answers

, doT :

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

- , - .

, Express 3.x, : dot-emc:

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

, . :

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"});
});
+5

express 3, . -:

npm install express-dot

app.set('view engine', 'dot' );
app.engine('dot', require('express-dot').__express );

:

res.render('profile', {}); // you will need to create views/profile.dot
+1

, , doT Express 4.x.x. - @olado, . ( , ), , include (#) :

var users = require('./routes/users');
// Standard app above this

var dot = require("dot").process({ 
  path: (__dirname + "/views")
});

var app = express();

// view engine setup
app.engine('dot', function(template, options, cb){
    // using .dot files
    var temp = path.parse(template).name;

    var cont = dot[temp](options);

    return cb(null, cont);


    // Or as one liner
    // return cb(null, dot[path.parse(template).name](options));

    // If you want to do error checking, return the error as callback functions first arg
    // return cb(new Error('Something went wrong');
});

app.set('views', path.join(__dirname, './views'));
app.set('view engine', 'dot');

// Standard generated app below this

"res.render" ( index.js):

/* GET home page. */
router.get('/', function(req, res, next) {
  res.render('index', { title: 'Express' });
});

{{it.value}} .dot. index.dot :

<!DOCTYPE html>
<html>
  <head>
    <title>{{=it.title}}</title>
    <link rel='stylesheet' href='/stylesheets/style.css' />
  </head>
  <body>
    <h1>{{=it.title}}</h1>
    <p>Welcome to {{=it.title}}</p>
  </body>
</html>
+1

All Articles