| 1 |
var fs = require('fs'); |
| 2 |
var path = require('path'); |
| 3 |
var walk = require('walk').walk; |
| 4 |
|
| 5 |
var async = require('./async'); |
| 6 |
|
| 7 |
function Instance(handlebars) { |
| 8 |
if (!(this instanceof Instance)) { |
| 9 |
return new Instance(handlebars); |
| 10 |
} |
| 11 |
|
| 12 |
// expose handlebars, allows users to use their versions |
| 13 |
// by overriding this early in their apps |
| 14 |
var self = this; |
| 15 |
|
| 16 |
self.handlebars = handlebars || require('handlebars').create(); |
| 17 |
|
| 18 |
// cache for templates, express 3.x doesn't do this for us |
| 19 |
self.cache = {}; |
| 20 |
|
| 21 |
self.__express = middleware.bind(this); |
| 22 |
|
| 23 |
// queue for partials registration |
| 24 |
self._queue = null |
| 25 |
|
| 26 |
// DEPRECATED, kept for backwards compatibility |
| 27 |
self.SafeString = this.handlebars.SafeString; |
| 28 |
self.Utils = this.handlebars.Utils; |
| 29 |
}; |
| 30 |
|
| 31 |
// express 3.x template engine compliance |
| 32 |
function middleware(filename, options, cb) { |
| 33 |
var self = this; |
| 34 |
|
| 35 |
if (self._queue) { |
| 36 |
self._queue.push(middleware.bind.apply(middleware, [this].concat(Array.prototype.slice.call(arguments)))) |
| 37 |
return |
| 38 |
} |
| 39 |
|
| 40 |
var cache = self.cache; |
| 41 |
var handlebars = self.handlebars; |
| 42 |
|
| 43 |
self.async = async(); |
| 44 |
|
| 45 |
// grab extension from filename |
| 46 |
// if we need a layout, we will look for one matching out extension |
| 47 |
var extension = path.extname(filename); |
| 48 |
|
| 49 |
// Default handlebars runtime options |
| 50 |
var handlebarsOpts = { |
| 51 |
allowProtoMethodsByDefault: true, |
| 52 |
allowProtoPropertiesByDefault: true |
| 53 |
} |
| 54 |
|
| 55 |
// If passing the locals as data, create the handlebars options object now |
| 56 |
if (self.__localsAsData) { |
| 57 |
handlebarsOpts.data = options._locals |
| 58 |
} |
| 59 |
|
| 60 |
// render the original file |
| 61 |
// cb(err, str) |
| 62 |
function render_file(locals, cb) { |
| 63 |
// cached? |
| 64 |
var template = cache[filename]; |
| 65 |
if (template) { |
| 66 |
try { |
| 67 |
var res = template(locals, handlebarsOpts) |
| 68 |
self.async.done(function (values) { |
| 69 |
Object.keys(values).forEach(function (id) { |
| 70 |
res = res.replace(id, values[id]) |
| 71 |
}) |
| 72 |
|
| 73 |
cb(null, res) |
| 74 |
}) |
| 75 |
} catch (err) { |
| 76 |
cb(prependFilenameToError(filename, err)) |
| 77 |
} |
| 78 |
|
| 79 |
return |
| 80 |
} |
| 81 |
|
| 82 |
fs.readFile(filename, 'utf8', function(err, str){ |
| 83 |
if (err) { |
| 84 |
return cb(err); |
| 85 |
} |
| 86 |
|
| 87 |
var template = handlebars.compile(str); |
| 88 |
if (locals.cache) { |
| 89 |
cache[filename] = template; |
| 90 |
} |
| 91 |
|
| 92 |
try { |
| 93 |
var res = template(locals, handlebarsOpts); |
| 94 |
self.async.done(function(values) { |
| 95 |
Object.keys(values).forEach(function(id) { |
| 96 |
res = res.replace(id, values[id]); |
| 97 |
}); |
| 98 |
|
| 99 |
cb(null, res); |
| 100 |
}); |
| 101 |
} catch (err) { |
| 102 |
cb(prependFilenameToError(filename, err)) |
| 103 |
} |
| 104 |
}); |
| 105 |
} |
| 106 |
|
| 107 |
// render with a layout |
| 108 |
function render_with_layout (filename, template, locals, cb) { |
| 109 |
render_file(locals, function(err, str) { |
| 110 |
if (err) { |
| 111 |
return cb(err); |
| 112 |
} |
| 113 |
|
| 114 |
locals.body = str; |
| 115 |
|
| 116 |
try { |
| 117 |
var res = template(locals, handlebarsOpts) |
| 118 |
self.async.done(function (values) { |
| 119 |
Object.keys(values).forEach(function (id) { |
| 120 |
res = res.replace(id, values[id]) |
| 121 |
}) |
| 122 |
|
| 123 |
cb(null, res) |
| 124 |
}) |
| 125 |
} catch (err) { |
| 126 |
cb(prependFilenameToError(filename, err)) |
| 127 |
} |
| 128 |
}); |
| 129 |
} |
| 130 |
|
| 131 |
var layout = options.layout; |
| 132 |
|
| 133 |
// user did not specify a layout in the locals |
| 134 |
// check global layout state |
| 135 |
if (layout === undefined && options.settings && options.settings['view options']) { |
| 136 |
layout = options.settings['view options'].layout; |
| 137 |
} |
| 138 |
|
| 139 |
// user explicitly request no layout |
| 140 |
// either by specifying false for layout: false in locals |
| 141 |
// or by settings the false view options |
| 142 |
if (layout !== undefined && !layout) { |
| 143 |
return render_file(options, cb); |
| 144 |
} |
| 145 |
|
| 146 |
var view_dirs = options.settings.views; |
| 147 |
|
| 148 |
var layout_filename = [].concat(view_dirs).map(function (view_dir) { |
| 149 |
var view_path = path.join(view_dir, layout || 'layout'); |
| 150 |
|
| 151 |
if (!path.extname(view_path)) { |
| 152 |
view_path += extension; |
| 153 |
} |
| 154 |
|
| 155 |
return view_path; |
| 156 |
}); |
| 157 |
|
| 158 |
for (var i = 0; i < layout_filename.length; i++) { |
| 159 |
var layout_template = cache[layout_filename[i]] |
| 160 |
|
| 161 |
if (layout_template) { |
| 162 |
return render_with_layout(layout_filename[i], layout_template, options, cb) |
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
// TODO check if layout path has .hbs extension |
| 167 |
|
| 168 |
function prependFilenameToError (filename, err) { |
| 169 |
// prepend to the message |
| 170 |
if (typeof err.message === 'string') { |
| 171 |
err.message = filename + ': ' + err.message |
| 172 |
} |
| 173 |
|
| 174 |
return err |
| 175 |
} |
| 176 |
|
| 177 |
function cacheAndCompile(filename, str) { |
| 178 |
var layout_template = handlebars.compile(str); |
| 179 |
if (options.cache) { |
| 180 |
cache[filename] = layout_template; |
| 181 |
} |
| 182 |
|
| 183 |
render_with_layout(filename, layout_template, options, cb) |
| 184 |
} |
| 185 |
|
| 186 |
function tryReadFileAndCache(templates) { |
| 187 |
var template = templates.shift(); |
| 188 |
|
| 189 |
fs.readFile(template, 'utf8', function(err, str) { |
| 190 |
if (err) { |
| 191 |
if (layout && templates.length === 0) { |
| 192 |
// Only return error if user explicitly asked for layout. |
| 193 |
return cb(err); |
| 194 |
} |
| 195 |
|
| 196 |
if (templates.length > 0) { |
| 197 |
return tryReadFileAndCache(templates); |
| 198 |
} |
| 199 |
|
| 200 |
return render_file(options, cb); |
| 201 |
} |
| 202 |
|
| 203 |
cacheAndCompile(template, str); |
| 204 |
}); |
| 205 |
} |
| 206 |
|
| 207 |
tryReadFileAndCache(layout_filename); |
| 208 |
} |
| 209 |
|
| 210 |
// express 2.x template engine compliance |
| 211 |
Instance.prototype.compile = function (str) { |
| 212 |
if (typeof str !== 'string') { |
| 213 |
return str; |
| 214 |
} |
| 215 |
|
| 216 |
var template = this.handlebars.compile(str); |
| 217 |
return function (locals) { |
| 218 |
return template(locals, { |
| 219 |
helpers: locals.blockHelpers, |
| 220 |
partials: null, |
| 221 |
data: null |
| 222 |
}); |
| 223 |
}; |
| 224 |
}; |
| 225 |
|
| 226 |
Instance.prototype.registerHelper = function () { |
| 227 |
this.handlebars.registerHelper.apply(this.handlebars, arguments); |
| 228 |
}; |
| 229 |
|
| 230 |
Instance.prototype.registerPartial = function () { |
| 231 |
this.handlebars.registerPartial.apply(this.handlebars, arguments); |
| 232 |
}; |
| 233 |
|
| 234 |
Instance.prototype.registerPartials = function (directory, options, done) { |
| 235 |
var self = this |
| 236 |
|
| 237 |
if (this._queue) { |
| 238 |
self._queue.unshift(self.registerPartials.bind.apply(self.registerPartials, |
| 239 |
[this].concat(Array.prototype.slice.call(arguments)))) |
| 240 |
return |
| 241 |
} else { |
| 242 |
self._queue = [] |
| 243 |
} |
| 244 |
|
| 245 |
var callback |
| 246 |
var handlebars = self.handlebars |
| 247 |
var opts = options || {} |
| 248 |
|
| 249 |
if (done || typeof options !== 'function') { |
| 250 |
callback = done |
| 251 |
} else { |
| 252 |
callback = options |
| 253 |
opts = {} |
| 254 |
} |
| 255 |
|
| 256 |
var rename = opts.rename !== undefined ? opts.rename : function (name) { |
| 257 |
return name.replace(/\-/g, '_') |
| 258 |
} |
| 259 |
|
| 260 |
var w = walk(directory) |
| 261 |
w.on('file', function (root, stat, done) { |
| 262 |
var filepath = path.join(root, stat.name) |
| 263 |
var isValidTemplate = /\.(html|hbs)$/.test(filepath); |
| 264 |
|
| 265 |
if (!isValidTemplate) { |
| 266 |
return done(null); |
| 267 |
} |
| 268 |
|
| 269 |
fs.readFile(filepath, 'utf8', function(err, data) { |
| 270 |
if (!err) { |
| 271 |
var extname = path.extname(filepath) |
| 272 |
var name = path.relative(directory, filepath) |
| 273 |
.slice(0, -(extname.length)) |
| 274 |
.replace(/\\/g, '/') |
| 275 |
|
| 276 |
handlebars.registerPartial(rename(name).replace(/ /g, '_'), data) |
| 277 |
} |
| 278 |
|
| 279 |
done(err); |
| 280 |
}); |
| 281 |
}) |
| 282 |
w.on('end', function () { |
| 283 |
if (self._queue) { |
| 284 |
var q = self._queue |
| 285 |
|
| 286 |
self._queue = null |
| 287 |
|
| 288 |
for (var i = 0; i < q.length; i++) { |
| 289 |
q[i]() |
| 290 |
} |
| 291 |
} |
| 292 |
}) |
| 293 |
|
| 294 |
if (callback) { |
| 295 |
w.on('end', callback) |
| 296 |
} |
| 297 |
}; |
| 298 |
|
| 299 |
Instance.prototype.registerAsyncHelper = function(name, fn) { |
| 300 |
var self = this; |
| 301 |
self.handlebars.registerHelper(name, function() { |
| 302 |
return self.async.resolve(fn.bind(this), arguments) |
| 303 |
}); |
| 304 |
}; |
| 305 |
|
| 306 |
Instance.prototype.localsAsTemplateData = function(app) { |
| 307 |
// Set a flag to indicate we should pass locals as data |
| 308 |
this.__localsAsData = true; |
| 309 |
|
| 310 |
app.render = (function(render) { |
| 311 |
return function(view, options, callback) { |
| 312 |
if (typeof options === "function") { |
| 313 |
callback = options; |
| 314 |
options = {}; |
| 315 |
} |
| 316 |
|
| 317 |
// Mix response.locals (options._locals) with app.locals (this.locals) |
| 318 |
options._locals = options._locals || {}; |
| 319 |
for (var key in this.locals) { |
| 320 |
options._locals[key] = this.locals[key]; |
| 321 |
} |
| 322 |
|
| 323 |
return render.call(this, view, options, callback); |
| 324 |
}; |
| 325 |
})(app.render); |
| 326 |
}; |
| 327 |
|
| 328 |
module.exports = new Instance(); |
| 329 |
module.exports.create = function(handlebars) { |
| 330 |
return new Instance(handlebars); |
| 331 |
}; |