Created
February 28, 2012 03:15
-
-
Save srohde/1929083 to your computer and use it in GitHub Desktop.
Node.js server-side compile Hogan.js templates
This file contains hidden or 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
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