doT
doT copied to clipboard
Lack of docs on pre-compiling doT template
As mentioned doT can be pre-compile, but I can't find any detailed introductions on that.
Hi @jiyinyiyong
Although I end up just relying on cache and not doing any precompiling, I think this is roughly how you would do it:
var dot = require('doT.js');
var template = '<html><body><h1>Header = {{=it.header}}</h1></body></html>';
var compiledTemplate = dot.compile(template);
// You can now use the compiled template as much as you want, e.g.:
console.log( compiledTemplate({header:'First Header' }) );
// = <html><body><h1>Header = First Header</h1></body></html>
console.log( compiledTemplate({ header:'Second Header' }) );
If you are creating an ExpressJS application, then on startup you could include an object with all your compiled templates and use them when needed (either compile on application start or pre-compile into an exported module just to be required on application startup).
For anything client-side, compile the templates on the server and create a .JS file to use on the client-side (with the compiled template functions).
Hi @jiyinyiyong I added some info in README today about pre-compilation with doT.process and CLI tool dot-packer. Please check it out. Hope this helps.
@sebowles51 Thanks. I'll check that later when I begin new projects. I was looking for a replacement for Handlebars on client-side, and later I found Jade could be pre-compiled too.
@olado Well, it's not detailed.. And the code, the render
function seems not ever been called:
var render = dot.process({
path: program.source,
destination: program.dest,
global: program.global
});
if (program.package) {
console.log("Packaging all files into " + program.package);
var fs = require("fs");
var files = [];
var dest = program.dest || './';
if (dest[dest.length-1] !== '/') dest += '/';
var sources = fs.readdirSync(dest);
for(k = 0; k < sources.length; k++) {
name = sources[k];
if (/\.js$/.test(name)) {
files.push(dest + name);
}
}
var result = require("uglify-js").minify(files);
fs.writeFileSync(program.package, result.code);
}
https://github.com/olado/doT/blob/master/bin/dot-packer#L31-L52
Seconded. Trying to figure out how to compile from .jst file just using api without auto compilation.
@jkarttunen my solution at last was to use React. The age for string-based template engines is gone.