Created
October 4, 2012 22:28
-
-
Save sheenobu/3836872 to your computer and use it in GitHub Desktop.
Trying my hand at node.js code with a documentation/readable code style.
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
/* | |
* vi: set syntax=javascript : | |
* vi: set tabstop=2 : | |
* vi: set expandtab : | |
*/ | |
var dust = require('dustjs-linkedin'), | |
fs = require("fs"), | |
Glob = require("glob").Glob, | |
path = require('path'), | |
async = require('async') | |
/* Load templates from the template folder, recursively. | |
* Templates are loaded asynchronously and with | |
* no guaranteed order. We can use async.forEachSeries | |
* if we need to guarantee order (which I'll be using elsewhere). | |
*/ | |
function load_templates(finished) { | |
new Glob("templates/**/*.html", {mark:false}, function(er,matches) { | |
async.forEach(matches, | |
/*iterator*/function(file,completed) { | |
fs.readFile(file, function(err,data) { | |
name = file.substring("templates/".length).replace(".html","") | |
dust.loadSource(dust.compile(String(data),name)) | |
completed(null) | |
})/*fs.readFile*/ | |
}/*iterator*/, | |
finished | |
)/*async.foreach*/ | |
})/*glob*/ | |
}/*load_templates*/ | |
load_templates(function(err) { | |
console.log("Finished loading templates") | |
dust.render("app1/index",{},function(err,out) { | |
console.log(out) // => <span>hello world</span> | |
})/*dust.render*/ | |
})/*load_templates*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment