Skip to content

Instantly share code, notes, and snippets.

@srohde
Created February 28, 2012 03:15
Show Gist options
  • Save srohde/1929083 to your computer and use it in GitHub Desktop.
Save srohde/1929083 to your computer and use it in GitHub Desktop.
Node.js server-side compile Hogan.js templates
Hogan = require 'hogan'
fs = require 'fs'
# The templates route generates the JavaScript file
# for the server side compiled Hogan templates.
exports.templates = (req, res) ->
compileTemplate = (template, callback) ->
filename = __dirname + '/../templates/' + template + '.hogan'
fs.readFile filename, (err, contents) ->
if err
throw err
else
temp = Hogan.compile contents.toString(), {asString : true}
callback '\nT.' + template + '=' + temp
templates = ['home', 'detail', 'search', 'settings']
result = 'T={};'
current = 0
for template in templates
compileTemplate template, (templateJS) ->
result += templateJS
# when all templates have been read
# send the genertate JS file back.
if ++current >= templates.length
res.contentType 'text/javascript'
res.send result
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment