Skip to content

Instantly share code, notes, and snippets.

@flockonus
Created August 9, 2012 16:05
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save flockonus/3305458 to your computer and use it in GitHub Desktop.
Save flockonus/3305458 to your computer and use it in GitHub Desktop.
swig on express.js 3.0
/**
* Module dependencies.
*/
var express = require('express')
, routes = require('./routes')
, http = require('http')
, path = require('path')
// !
, swig = require('./config/consolidate-swig').swig
var app = express();
if ('development' == app.get('env')) {
require('swig').init({ cache: false, allowErrors: true, filters: {} })
console.log('configuring DEV')
}
if ('production' == app.get('env')) {
require('swig').init({ cache: true, allowErrors: false, filters: {} })
console.log('configuring PROD')
}
app.configure(function(){
app.set('port', process.env.PORT || 3000);
// !!
app.engine('html', swig);
app.set('view engine', 'html');
app.set('view options', { layout: false });
app.use(express.favicon());
//app.use(express.logger('dev'));
app.use(express.logger({format: app.get('config').logger.format }));
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(express.cookieParser('yoursecrethere123123123123'));
app.use(express.session());
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));
});
app.configure('development', function(){
app.use(express.errorHandler());
});
app.get('/', routes.index);
http.createServer(app).listen(app.get('port'), function(){
console.log("Express server listening on port " + app.get('port'));
});
/*!
* consolidate
* Copyright(c) 2012 TJ Holowaychuk <tj@vision-media.ca>
* MIT Licensed
*
* Engines which do not support caching of their file contents
* should use the `read()` function defined in consolidate.js
* On top of this, when an engine compiles to a `Function`,
* these functions should either be cached within consolidate.js
* or the engine itself via `options.cache`. This will allow
* users and frameworks to pass `options.cache = true` for
* `NODE_ENV=production`, however edit the file(s) without
* re-loading the application in development.
*/
/**
* Module dependencies.
*/
var fs = require('fs');
/**
* Require cache.
*/
var cache = {};
/**
* Require cache.
*/
var requires = {};
/**
* Clear the cache.
*
* @api public
*/
exports.clearCache = function(){
cache = {};
};
/**
* Read `path` with `options` with
* callback `(err, str)`. When `options.cache`
* is true the template string will be cached.
*
* @param {String} options
* @param {Function} fn
* @api private
*/
function read(path, options, fn) {
var str = cache[path];
// cached (only if cached is a string and not a compiled template function)
if (options.cache && str && typeof str === 'string') return fn(null, str);
// read
fs.readFile(path, 'utf8', function(err, str){
if (err) return fn(err);
if (options.cache) cache[path] = str;
fn(null, str);
});
}
/**
* Swig support.
*/
exports.swig = function(path, options, fn){
var engine = requires.swig || (requires.swig = require('swig'));
read(path, options, function(err, str){
if (err) return fn(err);
try {
options.filename = path;
var tmpl = engine.compile(str, options);
fn(null, tmpl(options));
} catch (err) {
fn(err);
}
});
};
{
"name": "application-name",
"version": "0.0.1",
"private": true,
"scripts": {
"start": "node app"
},
"dependencies": {
"express": "3.0",
"swig": "0.12.0"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment