templates
Advanced tools
Comparing version 0.1.22 to 0.1.23
31
index.js
@@ -216,3 +216,3 @@ /*! | ||
collection: function (opts) { | ||
collection: function (opts, created) { | ||
if (!this.initialized) this.initialize(); | ||
@@ -233,3 +233,5 @@ var Views = this.get('Views'); | ||
this.emit('collection', collection, opts); | ||
this.extendViews(collection, opts); | ||
if (created !== true) { | ||
this.extendViews(collection, opts); | ||
} | ||
return collection; | ||
@@ -270,3 +272,3 @@ }, | ||
var collection = this.collection(opts); | ||
var collection = this.collection(opts, true); | ||
@@ -310,2 +312,3 @@ // get the collection inflections, e.g. page/pages | ||
this.emit('create', collection, opts); | ||
this.extendViews(collection, opts); | ||
@@ -327,3 +330,5 @@ // add collection and view helpers | ||
// decorate `option` method onto `view` | ||
utils.option(view); | ||
if (typeof view.option !== 'function') { | ||
utils.option(view); | ||
} | ||
@@ -365,2 +370,5 @@ // decorate `compile` method onto `view` | ||
app.extendView(view, opts); | ||
if (collection.options.engine) { | ||
view.option('engine', collection.options.engine); | ||
} | ||
app.handleView('onLoad', view); | ||
@@ -900,12 +908,10 @@ app.emit('view', view); | ||
// get the engine to use | ||
locals = utils.merge({settings: {}}, locals); | ||
var ext = locals.engine | ||
|| view.engine | ||
|| view.ext | ||
|| (view.ext = path.extname(view.path)); | ||
// get the engine to use | ||
var ext = utils.resolveEngine(view, locals, this.options); | ||
var engine = this.getEngine(ext); | ||
if (typeof engine === 'undefined') { | ||
throw this.error('compile', 'engine', view.ext); | ||
throw this.error('compile', 'engine', ext); | ||
} | ||
@@ -980,2 +986,3 @@ | ||
locals = utils.merge({}, this.cache.data, view.locals); | ||
var opts = this.options; | ||
@@ -986,4 +993,3 @@ // handle `preRender` middleware | ||
// get the engine | ||
var extname = view.ext || (view.ext = path.extname(view.path)); | ||
var ext = locals.engines || view.engine || extname; | ||
var ext = utils.resolveEngine(view, locals, opts); | ||
var engine = this.getEngine(ext); | ||
@@ -1008,3 +1014,2 @@ | ||
var opts = this.options; | ||
var ctx = view.context(locals); | ||
@@ -1011,0 +1016,0 @@ var context = this.context(view, ctx, locals); |
'use strict'; | ||
var List = require('../list'); | ||
var utils = require('../utils'); | ||
module.exports = function (app, collection, options) { | ||
var plural = options.plural; | ||
var single = options.inflection; | ||
/** | ||
* Create async helpers for each default template `type`. | ||
* Create sync helpers for each view collection | ||
* | ||
* @param {String} `type` The type of template. | ||
* @param {String} `plural` Plural form of `type`. | ||
* @param {String} `plural` Pluralized name of the collection. | ||
* @api private | ||
*/ | ||
app.helper(plural, function () { | ||
return app.getViews(plural); | ||
app.helper(options.plural, function (context) { | ||
var ctx = new List(collection); | ||
return context.fn(ctx); | ||
}); | ||
app.helper('views', function (name) { | ||
return app.getViews(name); | ||
}); | ||
}; |
@@ -8,2 +8,3 @@ 'use strict'; | ||
var single = options.inflection; | ||
var merge = utils.merge; | ||
@@ -14,12 +15,18 @@ // if `options` is an instance of `view`, get `view.options` | ||
} | ||
var viewType = options.viewType || []; | ||
/** | ||
* Get a specific view by `name`, optionally specifying | ||
* the collection to search as the second argument. | ||
* | ||
* @param {String} `name` | ||
* @param {String} `collection` | ||
* @return {String} | ||
* @api public | ||
*/ | ||
app.helper('view', function (name, type) { | ||
if (typeof type === 'string') { | ||
type = app.views[type]; | ||
} else { | ||
type = 'pages'; | ||
} | ||
return app.find(name, type); | ||
var view = app.find(name, type || 'pages'); | ||
return view ? view.content : ''; | ||
}); | ||
@@ -35,28 +42,24 @@ | ||
if (viewType.indexOf('partial') > -1) { | ||
app.asyncHelper(single, collectionHelper); | ||
app.helper(single, collectionHelper); | ||
} | ||
function collectionHelper(key, locals, opts, cb) { | ||
var args = [].slice.call(arguments, 1); | ||
cb = args.pop(); | ||
function collectionHelper(key, locals, opts) { | ||
try { | ||
var view = app.getView(plural, key); | ||
if (!Object.keys(view).length) { | ||
view = collection[key]; | ||
var view = collection.getView(key); | ||
if (!utils.isView(view)) { | ||
view = collection.views[key]; | ||
} | ||
if (!view || !Object.keys(view).length) { | ||
if (!utils.isView(view)) { | ||
app.emit('error', 'missing ' + single + ' `' + key + '`'); | ||
return cb(null, ''); | ||
return ''; | ||
} | ||
var locs = utils.getLocals.apply(utils.getLocals, args); | ||
locs = locs || {}; | ||
var ctx = utils.merge({}, this.context.view.locals, view.context(locs)); | ||
var ctx = merge({}, this.context); | ||
merge(ctx, view.locals); | ||
merge(ctx, view.data); | ||
merge(ctx, locals); | ||
view.render(ctx, function (err, res) { | ||
if (err) return cb(err); | ||
return cb(null, res.content); | ||
}); | ||
view.compile(this.context.view.options); | ||
return view.fn(ctx); | ||
@@ -66,5 +69,5 @@ } catch(err) { | ||
app.emit('error', err); | ||
cb(null, ''); | ||
return ''; | ||
} | ||
} | ||
}; |
@@ -272,3 +272,4 @@ 'use strict'; | ||
utils.isView = function isView(val) { | ||
return val && val.hasOwnProperty('content') | ||
if (!utils.isObject(val)) return false; | ||
return val.hasOwnProperty('content') | ||
|| val.hasOwnProperty('contents') | ||
@@ -317,2 +318,20 @@ || val.hasOwnProperty('path'); | ||
/** | ||
* Resolve the file extension to use for the engine. | ||
*/ | ||
utils.resolveEngine = function resolveEngine(view, locals, opts) { | ||
var engine = locals.engine | ||
|| view.options.engine | ||
|| view.engine | ||
|| view.data.engine | ||
|| (view.data.ext = path.extname(view.path)); | ||
var fn = opts.resolveEngine | ||
|| locals.resolveEngine | ||
|| utils.identity; | ||
return fn(engine); | ||
}; | ||
/** | ||
* Sync the _content and _contents properties on a view to ensure | ||
@@ -340,3 +359,2 @@ * both are set when setting one. | ||
view._contents = contents; | ||
// what should be done here? | ||
view._content = contents; | ||
@@ -343,0 +361,0 @@ } |
@@ -31,2 +31,3 @@ 'use strict'; | ||
this.data = view.data || {}; | ||
utils.option(view); | ||
@@ -33,0 +34,0 @@ this.define('_contents', null); |
{ | ||
"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.1.22", | ||
"version": "0.1.23", | ||
"homepage": "https://github.com/jonschlinkert/templates", | ||
@@ -53,3 +53,3 @@ "author": "Jon Schlinkert (https://github.com/jonschlinkert)", | ||
"consolidate": "^0.13.1", | ||
"engine-handlebars": "^0.6.1", | ||
"engine-handlebars": "^0.8.0", | ||
"event-stream": "^3.3.1", | ||
@@ -98,2 +98,2 @@ "for-own": "^0.1.3", | ||
} | ||
} | ||
} |
@@ -147,3 +147,3 @@ # templates [![NPM version](https://badge.fury.io/js/templates.svg)](http://badge.fury.io/js/templates) | ||
### [.create](index.js#L262) | ||
### [.create](index.js#L264) | ||
@@ -177,3 +177,3 @@ Create a new view collection that is stored on the `app.views` object. For example, if you create a collection named `posts`: | ||
### [.find](index.js#L402) | ||
### [.find](index.js#L410) | ||
@@ -197,3 +197,3 @@ Find a view by `name`, optionally passing a `collection` to limit the search. If no collection is passed all `renderable` collections will be searched. | ||
### [.getView](index.js#L440) | ||
### [.getView](index.js#L448) | ||
@@ -220,3 +220,3 @@ Get view `key` from the specified `collection`. | ||
### [.getViews](index.js#L479) | ||
### [.getViews](index.js#L487) | ||
@@ -240,3 +240,3 @@ Get all views from a `collection` using the collection's singular or plural name. | ||
### [.matchView](index.js#L511) | ||
### [.matchView](index.js#L519) | ||
@@ -262,3 +262,3 @@ Returns the first view from `collection` with a key that matches the given glob pattern. | ||
### [.matchViews](index.js#L539) | ||
### [.matchViews](index.js#L547) | ||
@@ -284,3 +284,3 @@ Returns any views from the specified collection with keys that match the given glob pattern. | ||
### [.handle](index.js#L581) | ||
### [.handle](index.js#L589) | ||
@@ -302,3 +302,3 @@ Handle a middleware `method` for `view`. | ||
### [.route](index.js#L670) | ||
### [.route](index.js#L678) | ||
@@ -328,3 +328,3 @@ Create a new Route for the given path. Each route contains a separate middleware stack. | ||
### [.all](index.js#L692) | ||
### [.all](index.js#L700) | ||
@@ -348,3 +348,3 @@ Special route method that works just like the `router.METHOD()` methods, except that it matches all verbs. | ||
### [.param](index.js#L721) | ||
### [.param](index.js#L729) | ||
@@ -373,3 +373,3 @@ Add callback triggers to route parameters, where `name` is the name of the parameter and `fn` is the callback function. | ||
### [.engine](index.js#L748) | ||
### [.engine](index.js#L756) | ||
@@ -398,3 +398,3 @@ Register a view engine callback `fn` as `ext`. | ||
### [.compile](index.js#L884) | ||
### [.compile](index.js#L892) | ||
@@ -424,3 +424,3 @@ Compile `content` with the given `locals`. | ||
### [.render](index.js#L950) | ||
### [.render](index.js#L956) | ||
@@ -444,3 +444,3 @@ Render a view with the given `locals` and `callback`. | ||
### [.mergePartials](index.js#L1030) | ||
### [.mergePartials](index.js#L1035) | ||
@@ -456,3 +456,3 @@ Merge "partials" view types. This is necessary for template | ||
### [.view](index.js#L1215) | ||
### [.view](index.js#L1221) | ||
@@ -707,3 +707,3 @@ Returns a new view, using the `View` class currently defined on the instance. | ||
### [.use](lib/view.js#L79) | ||
### [.use](lib/view.js#L80) | ||
@@ -726,3 +726,3 @@ Run a plugin on the `view` instance. | ||
### [.compile](lib/view.js#L100) | ||
### [.compile](lib/view.js#L101) | ||
@@ -745,3 +745,3 @@ Synchronously compile a view. | ||
### [.render](lib/view.js#L118) | ||
### [.render](lib/view.js#L119) | ||
@@ -763,3 +763,3 @@ Asynchronously render a view. | ||
### [.clone](lib/view.js#L152) | ||
### [.clone](lib/view.js#L153) | ||
@@ -976,3 +976,3 @@ Re-decorate View methods after calling vinyl's `.clone()` method. | ||
* `fn` **{Function}**: Plugin function. If the plugin returns a function it will be passed to the `use` method of each view created on the instance. | ||
* `fn` **{Function}**: Plugin function. | ||
* `returns` **{Object}**: Returns the instance for chaining. | ||
@@ -979,0 +979,0 @@ |
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
127105
3516