Skip to content

Instantly share code, notes, and snippets.

@simme
Created April 13, 2011 07:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save simme/917107 to your computer and use it in GitHub Desktop.
Save simme/917107 to your computer and use it in GitHub Desktop.
Welds data with HTML
/**
* @file
* Generates some various HTML stuffs.
*/
var fs = require('fs')
, jsdom = require('jsdom')
;
/**
* Welds some HTML together with a bunch of data
*
* @param template
* Name of the HTML-file to include, full path
* @param data
* Object where the key represents a selector and the value is data
* @param callback
* Callback that accepts the welded HTML as it's second argument and the
* error as the first.
*/
exports.weld = function (template, data, callback) {
// Setup our dom environment
jsdom.env(template,
[
__dirname + '/public/js/jquery.js',
require('weld').filepath
]
,
function(errors, window) {
if (errors) {
callback(errors, null);
}
// Do the welding
for (var i in data) {
window.weld(window.$(i)[0], data[i]);
}
// Remove script tags inserted by weld after </body>
window.$('script:last').remove();
window.$('script:last').remove();
callback(null, window.$('body').html());
}
);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment