Skip to content

Instantly share code, notes, and snippets.

@realistschuckle
Last active August 29, 2015 13:56
Show Gist options
  • Save realistschuckle/9165608 to your computer and use it in GitHub Desktop.
Save realistschuckle/9165608 to your computer and use it in GitHub Desktop.
jade View Engine for Yahoo! Mojito
/*jslint anon:true, sloppy:true, nomen:true, white:true*/
YUI.add('addons-viewengine-jade', function(Y, NAME) {
var jade = require('jade')
, fs = require('fs')
, cache = YUI.namespace('Env.Mojito.Jade')
;
function JadeAdapter(config) {
this.config = config || {};
}
JadeAdapter.prototype = {
render: function(data, mojitType, tmpl, adapter, meta, more) {
var engine = this
, tmplpath = tmpl['content-path']
;
this.compile(tmplpath, function(e, compiled) {
if(more) {
adapter.flush(compiled(data), meta);
} else {
adapter.done(compiled(data), meta);
}
});
}
, compile: function(tmplpath, callback) {
var cacheTemplates = (this.config.cacheTemplates === false)? false : true
;
if(!cacheTemplates || !cache[tmplpath]) {
fs.readFile(tmplpath, 'utf8', function(e, data) {
var jade = require('jade')
, compiled = jade.compile(data, {filename: tmplpath})
;
cache[tmplpath] = compiled;
callback(e, compiled);
});
} else {
callback(null, cache[tmplpath]);
}
}
};
Y.namespace('mojito.addons.viewEngines').jade = JadeAdapter;
}, "0.0.1");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment