Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 234
user Avatar asked Feb 09 '12 07:02

user


People also ask

How do node JS templating engines work?

js. As per the above figure, client-side browser loads HTML template, JSON/XML data and template engine library from the server. Template engine produces the final HTML using template and data in client's browser. However, some HTML templates process data and generate final HTML page at server side also.

What is doT template?

DOT extension are template files created by Microsoft Word to have pre-formatted settings for generation of further DOC files. A template file is created in order to have specific user settings that should be applied to subsequent files created from these.

Is it necessary to use template engine in node JS?

If we need to inject any data in an HTML file, we need a template engine. Also, Templating engines split our code into multiple components like header, footer, body, and so on.


2 Answers

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

app.set("view engine", "html");
app.register('.html', doT);
like image 124
olado Avatar answered Nov 14 '22 22:11

olado


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:

npm install dot-emc

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:

app.engine("def", require("dot-emc").__express);
app.set("view engine", "def");

Then you can start using it as you would any other view engine in your routes, e.g.:

app.get("/", function(req, res) {
    res.render("index", {"title": "title goes here"});
});
like image 34
cue8chalk Avatar answered Nov 15 '22 00:11

cue8chalk