Skip to content

Instantly share code, notes, and snippets.

@sheenobu
Created October 4, 2012 22:28
Show Gist options
  • Save sheenobu/3836872 to your computer and use it in GitHub Desktop.
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.
/*
* 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