dust.js is an asynchronous javascript template engine — you pass a callback to the “render” function.
CouchDB “show” functions, on the other hand, must actually return the HTML.
So I can’t use dust.js in my show function!…it just won’t work.
How can I get around this synchronous/asyncronous problem?
EDIT:
I should be able to do it by using the (asynchronous) “send” function:
function(doc,req){
var dust = require('lib/dust');
dust.renderSource('Hello, my name is {name}',{name:'Nick'},function(err,output){
send(output);
});
}
..but that is not working, I get this:
{"error":"render_error","reason":"function raised error: (new TypeError(\"dust.compile is not a function\", \"\", 37)) \nstacktrace: (\"Hello, my name is {name}\")@:37\n(\"Hello, my name is {name}\",[object Object],(function (err, output) {send(output);}))@:33\n([object Object],[object Object])@:2\napply([object Object],[object Array])@:0\nrunShow(function (doc, req) {var dust = require(\"lib/dust\");dust.renderSource(\"Hello, my name is {name}\", {name:\"Nick\"}, function (err, output) {send(output);});},[object Object],[object Array])@/usr/share/couchdb/server/main.js:886\n(function (doc, req) {var dust = require(\"lib/dust\");dust.renderSource(\"Hello, my name is {name}\", {name:\"Nick\"}, function (err, output) {send(output);});},[object Object],[object Array])@/usr/share/couchdb/server/main.js:989\napply(null,[object Array])@:0\n(\"_design/ibs_policies\",[object Array],[object Array])@/usr/share/couchdb/server/main.js:1401\napply(null,[object Array])@:0\n()@/usr/share/couchdb/server/main.js:1443\n@/usr/share/couchdb/server/main.js:1454\n"}
It seems that “dust.js” is not exposing all of it’s functions when imported as a CommonJS module?
Why is that?
How can I make this work?
Just based on a preliminary look at dust’s source, it seems to be expecting to find some nodejs-specific stuff if it’s not running in a browser. In particular, it looks like dust.compile is only exported from code that’s only called if process is defined and window isn’t. Neither will be defined when running under CouchDB’s view engine.