Skip to content

Instantly share code, notes, and snippets.

@matthewmueller
matthewmueller / Underscore templating with Express
Created March 25, 2011 01:52
Javascript: Use underscore templating to parse HTML files in Express
// Express with Underscore templating
app.register('.html', {
compile : function(str, options) {
return function(locals) {
return _.template(str, locals);
};
}
});
@matthewmueller
matthewmueller / gist:2018609
Created March 11, 2012 23:18
Javascript: after(len) - counter used for async iteration
/*
Counter used for async calls
Example:
var files = ['a', 'b'];
finished = after(files.length);
finished() // false
finished() // true
@matthewmueller
matthewmueller / gist:2018660
Created March 11, 2012 23:25
Javascript: step(fns...) - Tiny, flexible step function for running async functions in series
/*
Step - tiny, but flexible step library
Usage:
var first = function(next) {
...
return next(null, name, date);
}
#!/usr/bin/env node
var nom = require('nom'),
join = require('path').join,
args = process.argv.slice(2);
if(!args[0]) {
console.log('Usage: read <url>');
process.exit(0);
}
@matthewmueller
matthewmueller / express-layout.js
Created June 14, 2012 00:29
Basic, synchronous way of supporting layouts in express 3.x
app.engine('mu', function(path, options, fn) {
var views = app.get('views'),
view = path;
if(options.layout) {
view = join(views, '..', options.layout);
options.body = fs.readFileSync(path, 'utf8');
}
cons.hogan(view, options, function(err, str) {
@matthewmueller
matthewmueller / express-layout.js
Created June 14, 2012 00:30
Basic, synchronous way of supporting layouts in express 3.x
app.engine('mu', function(path, options, fn) {
var views = app.get('views'),
view = path;
if(options.layout) {
// Assumes layouts and views are at same level
view = join(views, '..', options.layout);
options.body = fs.readFileSync(path, 'utf8');
}
@matthewmueller
matthewmueller / index.js
Created June 24, 2012 22:05
Javascript: express 3 boilerplate
var express = require('express'),
cons = require('consolidate'),
port = process.argv[2] || 8080,
app = express();
app.engine('html', cons.hogan);
app.set('view engine', 'html');
app.set('views', __dirname);
app.configure(function() {
@matthewmueller
matthewmueller / toggle.js
Created August 12, 2012 07:14
Toggle between two or more functions
/**
* toggle between n functions
*
* Example:
*
* var a = function(p) { console.log("a:", p); };
* var b = function(p) { console.log("b:", p); };
*
* var func = toggle(a, b);
* func('hi') // => "a: hi"
@matthewmueller
matthewmueller / clean.js
Created August 22, 2012 22:56
Clean window
/**
* Written by Jonathan Bolster
* Blog Post: http://bolsterweb.com/2011/10/how-clean-is-your-window-object/
*/
(function(window){
var keys = {},
cleanWindow,
iframe = document.createElement("iframe");
iframe.style.display ="none";
@matthewmueller
matthewmueller / bind.js
Created August 25, 2012 02:32
super basic bind function
function __bind(fn, context) {
return function() {
return fn.apply(context, arguments);
};
}