It’s one of the fastest and concise javascript template engine for nodejs and web browsers, created by Laura Doktorova, the President & Co-Founder of Bebedo Inc.
Check out dot.js on Github.com.
dot.js works on both the client and server side. But for this tutorial, we’re only going to be focusing on the client-side.
There are many reasons to use a template engine rather than printing your code in a given language. I think overall, the main reasons for this are code consistency and organization. If you have a good separation of your markup from your logic it becomes easier to debug your code, to share your code with collaborators, and allows for more rapid development by streamlining common methods for outputting your markup.
Just include “doT.js” using the script tag. Once included, “doT” will be added to the global namespace.
var data = { name: "David" };
var template = "Hey {{=it.name}}, you own me money.";
var templateFunction = doT.template( template );
var html = templateFunction( data );
Full source:
var data = { name: "David" }; // step 1
var template = "Hey {{=it.name}}, you own me money."; // step 2
var templateFunction = doT.template( template ); // step 3
var html = templateFunction( data ); // step 4
// result
html == "Hey <b>David</b>, you own me money.";
Let’s now examine the function doT.template().
doT.template(template, settings, userDefinedDef);
The content of a template will remain the same, except for the sections that contain javascript encapulated sections (JES). JES look like this. "{{ }}".
Note that the data passed to the template function can be reference by the template using the namespace ‘it’.
Example:
// input
var data = { thatThingOverThere: "Bartman" };
var template = "Hey, look over there! It's {{=it.thatThingOverThere}}!";
var tempFunc = doT.template(template);
var html = tempFunc(data);
// output
html == "Hey, look over there! It's Bartman!";
To better understand what’s going on, let’s look at the generated source for the tempFunc.
// source for tempFunc
function anonymous(it) {
var out = 'Hey, look over there! It\'s '+(it.thatThingOverThere)+'!';
return out;
}
There two basic type of JES.
Type 1 requires a returned value.
var template = "{{=it.value}}";
Type 2 allows for javascript statements to be executed.
var template = "{{ var global = {}; }}";
Example:
var data = { team: "POWER RANGERS!"}
var template = '{{ var chant = "Go go"; }}{{= chant + " " + it.team}}';
var tempFunc = doT.template( template );
var html = tempFunc( data );
// result
html == "Go go POWER RANGERS!";
// source for tempFunc
function anonymous(it) {
var out='';
var chant = "Go go";
out+=( chant + " " + it.team);
return out;
}
What REALLY is Data Science? Told by a Data Scientist - By Joma Tech
Writing perfect code is a challenging process. That's where code reviews come in to help…
"The Next Leap: How A.I. will change the 3D industry - Andrew Price - Blender"
"Captain Disillusion: World's Greatest Blenderer - Live at the Blender Conference 2018 - CaptainDisillusion"
My 5 Favorite Linux Shell Tricks for SPEEEEEED (and efficiency) - By tutoriaLinux > What's…
View Comments
This is a very good example of doT template engine.
Thanks. I thought noone cared.
Nice article, Larry. Thank you. Btw, can I use doT.js with jQuery?
P.S. The link to Laura DoktorLova is broken!
Have you seen these results: http://jsperf.com/dom-vs-innerhtml-based-templating/562#comments
doT.js is way up there!
Thanks for your feedback. I fixed the broken link. And yes, you can use jQuery with doT.js.
I'll try to include an example of using jQuery with doT.js in the next tutorial.
Hi Larry,
First of all, I'm thankful to you for the simple and easy tutorial for someone who hardly knows anything about a 'template engine' - server or client side !
Eagerly looking forward for some more doT.js and as Olivier mentioned, its usage with JQuery !
Regards !
hi there,
thanks for your tutorial, I've got a question: it would be possible, using jQuery, to load the template from an external file?
http://stackoverflow.com/questions/4366137/load-jquery-templates-from-external-file
http://jsperf.com/out-of-dom-vs-documentfragment/23
Larry this article is very good and helpful for those who are new to doT.js, Thank you very much.
Could please post a real time example to load actual form template from external file and bind the data by doT engine?
Use ajax to load the external file then set the value as your `template` variable.
Related issue: http://stackoverflow.com/questions/11589387/load-txt-file-using-jquery-or-ajax
hi Larry, thank you for the intro to doT.js.
What doT.templateSettings { strip: true} do? does it trim the white spaces around the fields?
Hi Larry, we are using the doT at my work to generate narratives based on field values. Want to say thank you for putting together this example, it was pretty helpful.