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.
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.
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.
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.
You need to let express know to use doT as the template engine like this:
app.set("view engine", "html");
app.register('.html', doT);
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"});
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With