Created
August 12, 2013 23:16
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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