Skip to content

Instantly share code, notes, and snippets.

@DamienBell
Created August 12, 2013 23:16
Show Gist options
  • Save DamienBell/6216275 to your computer and use it in GitHub Desktop.
Save DamienBell/6216275 to your computer and use it in GitHub Desktop.
Compile a handlebars template without calling res.render On node typically use handlebars to render our webpage, but from time to time it's useful to compile a template from our views directory on the server as a string.
var express = require('express');
var fs = require('fs');
var hbs = require('hbs');
var app = module.exports = express.createServer();
app.configure(function(){
app.set('views', __dirname + '/views');
//app.set('view engine', 'jade');
app.set('view engine', 'hbs');
app.use(app.router);
app.use(express.static(__dirname + '/public'));
});
//let's read our layout straight from the views dir
var markup = fs.readFileSync("views/layout.hbs", "utf8");
//compile the template
var template = hbs.compile(markup);
//pass your object to the template fn()
console.log(template({}));
process.exit();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment