templates
Advanced tools
Comparing version 0.10.5 to 0.10.6
@@ -12,7 +12,5 @@ 'use strict'; | ||
var viewType = options.viewType || []; | ||
// get inflections | ||
var single = options.inflection; | ||
var plural = options.plural; | ||
var merge = utils.extend; | ||
var collection = app[plural]; | ||
@@ -30,3 +28,3 @@ | ||
app.helper('view', function(name, type) { | ||
app.helper('view', function(name, collectionName) { | ||
var last = utils.last(arguments); | ||
@@ -36,5 +34,5 @@ if (utils.isOptions(last)) { | ||
args.pop(); // drop hbs options object | ||
type = args.pop(); | ||
collectionName = args.pop(); | ||
} | ||
var view = app.find(name, type); | ||
var view = app.find(name, collectionName); | ||
return view && view.content || ''; | ||
@@ -50,5 +48,3 @@ }); | ||
if (viewType.indexOf('partial') > -1) { | ||
app.asyncHelper(single, createdHelper); | ||
} | ||
app.asyncHelper(single, viewHelper); | ||
@@ -59,17 +55,15 @@ /** | ||
* @param {String} `plural` Pluralized name of the collection. | ||
* @api private | ||
*/ | ||
app.asyncHelper(options.plural, function listHelper(context, cb) { | ||
app.asyncHelper(plural, function listHelper(context, cb) { | ||
if (typeof context === 'string') { | ||
createdHelper.apply(this, arguments); | ||
return; | ||
return viewHelper.apply(this, arguments); | ||
} | ||
var ctx = new List(collection); | ||
// render block helper with list as context | ||
if (typeof context.fn === 'function') { | ||
cb(null, context.fn(ctx)); | ||
return; | ||
return cb(null, context.fn(ctx)); | ||
} | ||
// return list when not used as a block helper | ||
@@ -83,15 +77,13 @@ cb(null, ctx); | ||
* @param {String} `plural` Pluralized name of the collection. | ||
* @api private | ||
*/ | ||
function createdHelper(name, locals, options, cb) { | ||
function viewHelper(name, locals, options, cb) { | ||
app.emit('helper', single + ' helper > rendering "' + name + '"'); | ||
if (typeof locals === 'function') { | ||
cb = locals; | ||
options = {}; | ||
locals = {}; | ||
return viewHelper.call(this, name, {}, {}, locals); | ||
} | ||
if (typeof options === 'function') { | ||
cb = options; | ||
options = {}; | ||
return viewHelper.call(this, name, locals, {}, options); | ||
} | ||
@@ -102,31 +94,10 @@ | ||
var hash = options.hash || locals.hash || {}; | ||
var opts = utils.extend({}, this.options, hash); | ||
var opts = helperOptions.call(this, locals, options); | ||
var view = collection.getView(name, opts); | ||
if (opts.verbose === true) { | ||
console.log(single + ' helper rendering: "' + name + '"'); | ||
} | ||
if (!view) { | ||
app.emit('error', new Error('helper "' + single + '" cannot find "' + name + '"')); | ||
return cb(null, ''); | ||
return utils.helperError(app, single, name, cb); | ||
} | ||
var ctx = {}; | ||
var fn = this.options.helperContext; | ||
if (typeof fn === 'function') { | ||
ctx = fn.call(this, view, locals, options); | ||
} else { | ||
// context | ||
ctx = merge({}, ctx, this.context.view.data); | ||
ctx = merge({}, ctx, this.context); | ||
var ctx = helperContext.call(this, view, locals, opts); | ||
// view | ||
ctx = merge({}, ctx, view.locals, view.data); | ||
// locals and options | ||
ctx = merge({}, ctx, locals, hash); | ||
} | ||
this.app.render(view, ctx, function(err, res) { | ||
@@ -138,1 +109,58 @@ if (err) return cb(err); | ||
}; | ||
/** | ||
* Create an options object from: | ||
* | ||
* - helper `locals` | ||
* - helper `options` | ||
* - options `hash` if it's registered as a handlebars helper | ||
* - context options (`this.options`), created from options define on `app.option()`. | ||
* | ||
* @param {Object} `locals` | ||
* @param {Object} `options` | ||
* @return {Object} | ||
*/ | ||
function helperOptions(locals, options) { | ||
var hash = options.hash || locals.hash || {}; | ||
var opts = utils.extend({}, this.options, hash); | ||
opts.hash = hash; | ||
return opts; | ||
} | ||
/** | ||
* Create the context to use for rendering from: | ||
* | ||
* - helper `locals` | ||
* - helper `options` | ||
* - context (`this.context`) | ||
* - `options.hash` if it's registered as a handlebars helper | ||
* - `view.locals`: locals defined on the view being rendered | ||
* - `view.data`: data from front-matter | ||
* | ||
* @param {Object} `view` the view being rendered | ||
* @param {Object} `locals` Helper locals | ||
* @param {Object} `options` Helper options | ||
* @return {Object} | ||
*/ | ||
function helperContext(view, locals, options) { | ||
var fn = this.options.helperContext; | ||
var extend = utils.extend; | ||
var context = {}; | ||
if (typeof fn === 'function') { | ||
context = fn.call(this, view, locals, options); | ||
} else { | ||
// helper context | ||
context = extend({}, this.context.view.data); | ||
context = extend({}, context, this.context); | ||
// view context | ||
context = extend({}, context, view.locals, view.data); | ||
// helper locals and options | ||
context = extend({}, context, locals, options.hash); | ||
} | ||
return context; | ||
} |
'use strict'; | ||
var common = require('./common'); | ||
/** | ||
@@ -10,2 +12,11 @@ * Expose helper utils | ||
/** | ||
* Return true if the given value is an object. | ||
* @return {Boolean} | ||
*/ | ||
utils.isOptions = function(val) { | ||
return common.isObject(val) && val.hasOwnProperty('hash'); | ||
}; | ||
/** | ||
* Bind a `thisArg` to all the functions on the target | ||
@@ -31,1 +42,16 @@ * | ||
}; | ||
/** | ||
* Format a helper error. | ||
* TODO: create an error class for helpers | ||
*/ | ||
utils.helperError = function(app, helperName, viewName, cb) { | ||
var err = new Error('helper "' + helperName + '" cannot find "' + viewName + '"'); | ||
app.emit('error', err); | ||
if (typeof cb === 'function') { | ||
return cb(err); | ||
} else { | ||
throw err; | ||
} | ||
}; |
@@ -13,11 +13,2 @@ 'use strict'; | ||
/** | ||
* Return true if the given value is an object. | ||
* @return {Boolean} | ||
*/ | ||
utils.isOptions = function(val) { | ||
return common.isObject(val) && val.hasOwnProperty('hash'); | ||
}; | ||
/** | ||
* Return true if the given value looks like a | ||
@@ -24,0 +15,0 @@ * `view` object. |
'use strict'; | ||
var fs = require('fs'); | ||
var path = require('path'); | ||
@@ -241,3 +242,2 @@ var plugin = require('./plugins'); | ||
for (var key in this.views) { | ||
@@ -260,2 +260,9 @@ view = this.views[key]; | ||
} | ||
if (fs.existsSync(name)) { | ||
return this.addView(name, { | ||
contents: fs.readFileSync(name) | ||
}); | ||
} | ||
return ''; | ||
@@ -262,0 +269,0 @@ }; |
{ | ||
"name": "templates", | ||
"description": "System for creating and managing template collections, and rendering templates with any node.js template engine. Can be used as the basis for creating a static site generator or blog framework.", | ||
"version": "0.10.5", | ||
"version": "0.10.6", | ||
"homepage": "https://github.com/jonschlinkert/templates", | ||
@@ -6,0 +6,0 @@ "author": "Jon Schlinkert (https://github.com/jonschlinkert)", |
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
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
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
121162
3220
3