Skip to content

Instantly share code, notes, and snippets.

@tcr
Created October 26, 2011 06:22
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save tcr/1315609 to your computer and use it in GitHub Desktop.
Save tcr/1315609 to your computer and use it in GitHub Desktop.
How can you use Mustache with Node.js?
var express = require('express');
var app = module.exports = express.createServer();
// Configuration
app.configure(function(){
app.set('view engine', 'mustache');
app.set('views', __dirname + '/views');
app.register(".mustache", require('stache'));
app.use(express.static(__dirname + '/public'));
});
app.configure('development', function(){
app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
});
app.configure('production', function(){
app.use(express.errorHandler());
});
app.get('/', function(req, res){
res.render('index', {
locals: {
title: 'Hello world',
person: 'Partner'
}
});
});
app.listen(3000);
console.log("Express server listening on port %d in %s mode", app.address().port, app.settings.env);

Just npm install express mustache and you're up and running. Express works with stache (which includes mustache.js), though Google seems to insist the Mu library rank higher.

mustache doesn't support express natively, but stache is an up-to-date wrapper that supports layouts.

["js","node.js","mustache","template"]
http://stackoverflow.com/questions/7531290/nodejs-mustache-handlebars-example-project
@dundee
Copy link

dundee commented Dec 28, 2011

This really works for you?

I get only error TypeError: Object # has no method 'compile' at node_modules/express/lib/view.js:65:33.
Mustache object has no method compile.

@tcr
Copy link
Author

tcr commented Dec 28, 2011

Just tried it again, no such error. express@2.4.7 and stache@0.1.0 ?

@dundee
Copy link

dundee commented Dec 29, 2011

Sorry, my fault :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment