Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lack of docs on pre-compiling doT template #87

Open
tiye opened this issue Aug 12, 2013 · 5 comments
Open

Lack of docs on pre-compiling doT template #87

tiye opened this issue Aug 12, 2013 · 5 comments

Comments

@tiye
Copy link

tiye commented Aug 12, 2013

As mentioned doT can be pre-compile, but I can't find any detailed introductions on that.

@sebowles51
Copy link

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).

@olado
Copy link
Owner

olado commented Aug 24, 2013

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.

@tiye
Copy link
Author

tiye commented Aug 24, 2013

@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

@jkarttunen
Copy link

Seconded. Trying to figure out how to compile from .jst file just using api without auto compilation.

@tiye
Copy link
Author

tiye commented Apr 14, 2015

@jkarttunen my solution at last was to use React. The age for string-based template engines is gone.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants