baryshev / ect

Fastest JavaScript template engine with embedded CoffeeScript syntax
http://ectjs.com
MIT License
626 stars 69 forks source link

Comiler function forgetting compiled version of template #96

Open im4LF opened 9 years ago

im4LF commented 9 years ago

I need compiler function without http senders:


renderer.get('path/to/template', function (error, res) {
    // res - compiled version of path/to/template.ect
})

And amd loader without coffee-script dependencies for server-side compiling

im4LF commented 9 years ago

Something like:

this.compiled = function (template, callback) {

    try {
        var context = new TemplateContext();
        var container = context.load(template);

        callback(null, container);
    }
    catch (err) {
        callback(err);
    }
};

And usage:

renderer.compiled('path/to/template', function (err, res) {
    // do something with res.source
});
Dexus commented 8 years ago

Why you dont Use:

var ECT = require('ect');

var renderer = ECT({ root : __dirname + '/views', ext : '.ect' });

renderer.render('page', { title: 'Hello, World!' }, function (error, html) {
    console.log(error);
    console.log(html);
});

Or what I miss to understand?

im4LF commented 8 years ago

I need compiled version of template not a rendered result

Dexus commented 8 years ago

ah, sorry. but its also possible todo:

var tpl_compiled = {};
var ECT = require('ect');

var renderer = ECT({ root : __dirname + '/views', ext : '.ect' });

if (tpl_compiled['page']){
    return tpl_compiled['page'];
}
else
{
renderer.render('page', { title: 'Hello, World!' }, function (error, html) {
    console.log(error);
    console.log(html);
    tpl_compiled['page'] = html;
});
}
im4LF commented 8 years ago

I mean translate cofeescript to native javascript

var template = '{{ if @a > 10: }}more then 10{{ end }}';
var compiled = '...  if ( this.a > 10 ) { buf.push("more then 10") } ...';
baryshev commented 8 years ago

You can compile ECT templates to JS with bundled cli tool (located in bin directory), and use precompiled templates.