Created
April 13, 2011 07:09
-
-
Save simme/917107 to your computer and use it in GitHub Desktop.
Welds data with HTML
This file contains 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
/** | |
* @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