broadway-handlebars
Advanced tools
Comparing version 0.1.5 to 0.1.7
35
index.js
@@ -15,15 +15,18 @@ var _ = require('lodash'), | ||
bwHandlebars.prototype.attach = function (options) { | ||
var defaults = { | ||
helpers: {}, | ||
optimize: false, | ||
development: true | ||
}, | ||
// support the factory if exists - https://github.com/tommydudebreaux/handlebars.js | ||
// otherwise, use the default singleton approach here. | ||
Handlebars = handlebars.create ? handlebars.create() : handlebars; | ||
var Handlebars = handlebars.create ? handlebars.create() : handlebars; | ||
_.extend(defaults, options); | ||
_.defaults(options, { | ||
helpers: {}, | ||
optimize: false, | ||
development: true, | ||
view: { | ||
path: process.cwd(), | ||
ext: "html" | ||
} | ||
}); | ||
var viewResolver = options.viewResolver || new defaultViewResolver(), | ||
var viewResolver = options.viewResolver || new defaultViewResolver(options.view), | ||
templateCache = {}; | ||
@@ -37,3 +40,8 @@ | ||
_.each(context, function(v, k) { | ||
buffer.push(options.fn({ key: k, value: v })); | ||
try { | ||
buffer.push(options.fn({ key: k, value: v })); | ||
} | ||
catch (e) { | ||
buffer.push(e); | ||
} | ||
}); | ||
@@ -52,3 +60,8 @@ | ||
_.each(range, function(v, k) { | ||
buffer.push(options.fn({ key: k, value: v })); | ||
try { | ||
buffer.push(options.fn({ key: k, value: v })); | ||
} | ||
catch (e) { | ||
buffer.push(e); | ||
} | ||
}); | ||
@@ -136,3 +149,3 @@ | ||
templateCache[view] = Handlebars.compile(markup); | ||
templateCache[view] = Handlebars.compile(markup || "View Not Found - " + view); | ||
return templateCache[view]; | ||
@@ -139,0 +152,0 @@ }; |
{ | ||
"name": "broadway-handlebars", | ||
"version": "0.1.5", | ||
"version": "0.1.7", | ||
"main": "index", | ||
@@ -31,3 +31,3 @@ "description": "Plugin for flatiron/broadway for rendering with the handlebars view engine.", | ||
"scripts": { | ||
"test": "node /tests/example.js" | ||
"test": "node tests/example.js" | ||
}, | ||
@@ -34,0 +34,0 @@ "dependencies": { |
@@ -5,7 +5,10 @@ var http = require('http'), | ||
var app = new broadway.App(); | ||
app.use(new bwHandlebars(), { /* defaults are ok so empty being passed */ }); | ||
app.use(new bwHandlebars(), { | ||
view: { | ||
base: ["./tests/overrides", "./tests/default"] | ||
} | ||
}); | ||
app.render('tests/index', { | ||
app.render('index', { | ||
languages: { | ||
@@ -23,4 +26,5 @@ spanish: { | ||
}, function(err, content) { | ||
console.log(err || ''); | ||
console.log(content); | ||
process.exit(); | ||
}); |
@@ -8,28 +8,16 @@ var _ = require('lodash'), | ||
var ViewResolver = function(options) { | ||
var defaults = { | ||
this.options = _.defaults(options, { | ||
base: process.cwd(), | ||
ext: "html" | ||
}; | ||
this.options = _.extend(defaults, options); | ||
this.regexExt = new RegExp("\\." + this.options.ext + "$"); | ||
}; | ||
}); | ||
/** | ||
* Retrieves the markup for a given view. | ||
* | ||
**/ | ||
ViewResolver.prototype.get = function(name, callback) { | ||
fs.readFile(this.options.base + name, 'utf-8', callback); | ||
if (!(this.options.base instanceof Array)) { | ||
this.options.base = [this.options.base]; | ||
} | ||
}; | ||
/** | ||
* Retrieves the markup for all views in a given folder. | ||
* Return will be in format of | ||
* key: path relative to the base | ||
* val: the contents of the file | ||
**/ | ||
ViewResolver.prototype.all = function(callback) { | ||
var that = this, | ||
var getAll = function(base, ext, callback) { | ||
var regexExt = new RegExp("\\." + ext + "$"), | ||
dict = {}, | ||
basePath = path.normalize(this.options.base); | ||
basePath = path.normalize(base); | ||
@@ -43,3 +31,3 @@ // walk the contents of the base path | ||
// ...if this is a file of the expected extension... | ||
if (that.regexExt.test(file)) { | ||
if (regexExt.test(file)) { | ||
// ... then hold onto the path | ||
@@ -67,3 +55,3 @@ paths.push(file); | ||
// simplify the key to be relative to base path and omit file extension | ||
var key = path.replace(basePath + '/', '').replace(that.regexExt, ''); | ||
var key = path.replace(basePath + '/', '').replace(regexExt, ''); | ||
dict[key] = data; | ||
@@ -88,2 +76,27 @@ callback(); | ||
/** | ||
* Retrieves the markup for all views in a given folder. | ||
* Return will be in format of | ||
* key: path relative to the base | ||
* val: the contents of the file | ||
**/ | ||
ViewResolver.prototype.all = function(callback) { | ||
var that = this, | ||
ops = _.map(this.options.base, function(base) { | ||
return function(cb) { | ||
getAll(base, that.options.ext, cb); | ||
}; | ||
}); | ||
async.parallel(ops.reverse(), function(err, results) { | ||
var dict = results.splice(0, 1)[0]; | ||
_.each(results, function(r) { | ||
_.extend(dict, r); | ||
}); | ||
callback(null, dict); | ||
}); | ||
}; | ||
module.exports = ViewResolver; |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
9157
9
244