Last active
December 22, 2015 11:08
-
-
Save carchrae/6463103 to your computer and use it in GitHub Desktop.
javascript verticle for template rendering with dust.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var vertx = require("vertx.js") | |
var console = require('vertx/console'); | |
console.log('module ' + JSON.stringify(module)); | |
var dust; | |
var x = function(){ | |
var module = undefined; | |
var exports = undefined; | |
load("dustjs/dust-full-1.2.6.js"); | |
console.log('module ' + JSON.stringify(module)); | |
dust = getGlobal(); | |
load("dustjs/dust-helpers-1.1.1.js"); | |
console.log('module ' + JSON.stringify(module)); | |
return dust; | |
}(); | |
var main = function() { | |
try { | |
console.log('started..'); | |
var eb = vertx.eventBus; | |
console.log('dust'); | |
var compileHandler = function(m, reply) { | |
try { | |
console.log(JSON.stringify(m)); | |
console.log('compileHandler = ' + JSON.stringify(dust)); | |
m.compiled = dust.compile(m.source, m.name); | |
dust.loadSource(m.compiled); | |
reply(m); | |
} catch (e) { | |
console.log(e); | |
console.log(e.stack); | |
} | |
} | |
var loadHandler = function(m, reply) { | |
try { | |
console.log(JSON.stringify(m)); | |
m.compiled = dust.compileFn(m.source, m.name); | |
reply(m); | |
} catch (e) { | |
console.log(e); | |
console.log(e.stack); | |
} | |
} | |
var renderHandler = function(m, reply) { | |
console.log(JSON.stringify(m)); | |
try { | |
console.log(JSON.stringify(m)); | |
dust.render(m.name, m.context, function(err, out) { | |
console.log('err=' + err); | |
console.log('out=' + out); | |
if (err) { | |
reply({ | |
error : err | |
}); | |
} else | |
reply({ | |
output : out | |
}); | |
}); | |
} catch (e) { | |
console.log(e); | |
console.log(e.stack); | |
} | |
} | |
eb.registerHandler('dust.compile', compileHandler); | |
eb.registerHandler('dust.load', loadHandler); | |
eb.registerHandler('dust.render', renderHandler); | |
eb.onopen = function() { | |
console.log('open'); | |
} | |
eb.onclosed = function() { | |
console.log('closed'); | |
} | |
eb.send('dust.compile', { | |
name : 'test', | |
source : "hello {x}!" | |
}, function reply(m) { | |
console.log(m.compiled); | |
eb.send('dust.render', { | |
name : 'test', | |
context : { | |
x : 'world' | |
} | |
}, function(m2) { | |
console.log('response: ' + m2.output); | |
}); | |
}); | |
} catch (e) { | |
console.log(e); | |
console.log(e.stack); | |
} | |
console.log('done'); | |
}(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment