templates
Advanced tools
Comparing version 0.1.12 to 0.1.13
64
index.js
@@ -95,3 +95,3 @@ /*! | ||
initialize: function () { | ||
this.define('Base', require('base-methods')); | ||
this.define('Base', Base); | ||
this.define('View', this.options.View || View); | ||
@@ -130,13 +130,14 @@ this.define('List', this.options.List || List); | ||
/** | ||
* Create a view collection. View collections are stored | ||
* on the `app.views` object. For example, if you create a collection | ||
* named `posts`, the all `posts` will be stored on `app.views.posts`. | ||
* Create a new view collection. View collections are decorated | ||
* with special methods for getting, setting and rendering | ||
* views from that collection. Collections created with this method | ||
* are not stored on `app.views` as with the [create](#create) method. | ||
* | ||
* ```js | ||
* app.create('posts'); | ||
* app.posts({...}); // add an object of views | ||
* app.post('foo', {content: '...'}); // add a single view | ||
* var collection = app.collection(); | ||
* collection.addViews({...}); // add an object of views | ||
* collection.addView('foo', {content: '...'}); // add a single view | ||
* | ||
* // collection methods are chainable too | ||
* app.post('home.hbs', {content: 'foo <%= title %> bar'}) | ||
* collection.addView('home.hbs', {content: 'foo <%= title %> bar'}) | ||
* .render({title: 'Home'}, function(err, res) { | ||
@@ -146,15 +147,14 @@ * //=> 'foo Home bar' | ||
* ``` | ||
* @name .create | ||
* @param {String} `name` The name of the collection. Plural or singular form. | ||
* @name .collection | ||
* @param {Object} `opts` Collection options | ||
* @param {String|Array|Function} `loaders` Loaders to use for adding views to the created collection. | ||
* @return {Object} Returns the `Template` instance for chaining. | ||
* @return {Object} Returns the `collection` instance for chaining. | ||
* @api public | ||
*/ | ||
create: function(name, opts) { | ||
collection: function (opts) { | ||
if (!this.initialized) this.initialize(); | ||
var collection = null; | ||
var Views = this.get('Views'); | ||
var collection = null; | ||
if (opts instanceof this.Views) { | ||
if (opts instanceof Views) { | ||
collection = opts; | ||
@@ -165,8 +165,38 @@ opts = {}; | ||
opts.View = opts.View || this.get('View'); | ||
collection = new this.Views(opts); | ||
collection = new Views(opts); | ||
} | ||
// pass the `View` constructor from `App` to the collection | ||
collection = this.extendViews(collection, opts); | ||
return this.extendViews(collection, opts); | ||
}, | ||
/** | ||
* Create a new view collection that is stored on the `app.views` object. | ||
* For example, if you create a collection named `posts`, then all `posts` will be | ||
* stored on `app.views.posts`, and a `posts` method will be added to | ||
* `app`, allowing you to add posts to the collection using `app.posts()`. | ||
* | ||
* ```js | ||
* app.create('posts'); | ||
* app.posts({...}); // add an object of views | ||
* app.post('foo', {content: '...'}); // add a single view | ||
* | ||
* // collection methods are chainable too | ||
* app.post('home.hbs', {content: 'foo <%= title %> bar'}) | ||
* .render({title: 'Home'}, function(err, res) { | ||
* //=> 'foo Home bar' | ||
* }); | ||
* ``` | ||
* @name .create | ||
* @param {String} `name` The name of the collection. Plural or singular form. | ||
* @param {Object} `opts` Collection options | ||
* @param {String|Array|Function} `loaders` Loaders to use for adding views to the created collection. | ||
* @return {Object} Returns the `collection` instance for chaining. | ||
* @api public | ||
*/ | ||
create: function(name, opts) { | ||
opts = opts || {}; | ||
var collection = this.collection(opts); | ||
// get the collection inflections, e.g. page/pages | ||
@@ -173,0 +203,0 @@ var single = utils.single(name); |
'use strict'; | ||
var path = require('path'); | ||
var glob = require('globby'); | ||
var utils = require('./utils'); | ||
@@ -10,2 +12,5 @@ | ||
var async = app._.helpers.async; | ||
var sync = app._.helpers.sync; | ||
app.visit('mixin', { | ||
@@ -21,10 +26,4 @@ | ||
helper: function(name, fn) { | ||
if (typeof name === 'string' && arguments.length === 1) { | ||
return this._.helpers.sync.getHelper(name); | ||
} | ||
if (typeof fn !== 'function') { | ||
throw new TypeError('expected helper fn to be a function.'); | ||
} | ||
this._.helpers.sync.addHelper(name, fn); | ||
helper: function() { | ||
this.helpers.apply(this, arguments); | ||
return this; | ||
@@ -34,18 +33,15 @@ }, | ||
/** | ||
* Get or set an async helper. If only the name is passed, the | ||
* helper is returned. | ||
* Register multiple template helpers. | ||
* | ||
* @param {String} `name` Helper name. | ||
* @param {Function} `fn` Helper function | ||
* @param {Object|Array} `helpers` Object, array of objects, or glob patterns. | ||
* @api public | ||
*/ | ||
asyncHelper: function(name, fn) { | ||
if (typeof name === 'string' && arguments.length === 1) { | ||
return this._.helpers.async.getHelper(name); | ||
helpers: function() { | ||
var cache = {}; | ||
var loader = utils.loader(cache); | ||
loader.apply(loader, arguments); | ||
for (var key in cache) { | ||
sync.addHelper(key, cache[key]); | ||
} | ||
if (typeof fn !== 'function') { | ||
throw new TypeError('expected helper fn to be a function.'); | ||
} | ||
this._.helpers.async.addAsyncHelper(name, fn); | ||
return this; | ||
@@ -55,30 +51,12 @@ }, | ||
/** | ||
* Register multiple template helpers. | ||
* Get or set an async helper. If only the name is passed, the | ||
* helper is returned. | ||
* | ||
* @param {Object|Array} `helpers` Object, array of objects, or glob patterns. | ||
* @param {String} `name` Helper name. | ||
* @param {Function} `fn` Helper function | ||
* @api public | ||
*/ | ||
helpers: function(helpers, options) { | ||
if (helpers && utils.isValidGlob(helpers) && utils.hasGlob(helpers)) { | ||
helpers = utils.requireGlob(helpers, options || {}); | ||
if (!Object.keys(helpers).length) return this; | ||
for (var name in helpers) { | ||
if (helpers.hasOwnProperty(name)) { | ||
var val = helpers[name]; | ||
if (typeof val === 'function') { | ||
this.helper(name, val); | ||
} else if (typeof val === 'object') { | ||
this.helpers(val); | ||
} | ||
} | ||
} | ||
return this; | ||
} | ||
if (typeof helpers !== 'object') { | ||
throw new TypeError('expected helpers to be an object.'); | ||
} | ||
this.visit('helper', helpers); | ||
asyncHelper: function() { | ||
this.asyncHelpers.apply(this, arguments); | ||
return this; | ||
@@ -94,24 +72,9 @@ }, | ||
asyncHelpers: function(helpers, options) { | ||
if (helpers && utils.isValidGlob(helpers) && utils.hasGlob(helpers)) { | ||
helpers = utils.requireGlob(helpers, options || {}); | ||
if (!Object.keys(helpers).length) return this; | ||
for (var name in helpers) { | ||
if (helpers.hasOwnProperty(name)) { | ||
var val = helpers[name]; | ||
if (typeof val === 'function') { | ||
this.asyncHelper(name, val); | ||
} else if (typeof val === 'object') { | ||
this.asyncHelpers(val); | ||
} | ||
} | ||
} | ||
return this; | ||
asyncHelpers: function() { | ||
var cache = {}; | ||
var loader = utils.loader(cache); | ||
loader.apply(loader, arguments); | ||
for (var key in cache) { | ||
async.addAsyncHelper(key, cache[key]); | ||
} | ||
if (typeof helpers !== 'object') { | ||
throw new TypeError('expected helpers to be an object.'); | ||
} | ||
this.visit('asyncHelper', helpers); | ||
return this; | ||
@@ -118,0 +81,0 @@ } |
'use strict'; | ||
var define = require('define-property'); | ||
var Base = require('base-methods'); | ||
@@ -22,3 +21,3 @@ var utils = require('./utils'); | ||
} | ||
}; | ||
} | ||
@@ -25,0 +24,0 @@ Base.extend(List); |
@@ -26,2 +26,3 @@ 'use strict'; | ||
// engines, templates and helpers | ||
lazy('load-helpers', 'loader'); | ||
lazy('engine-base', 'engine'); | ||
@@ -95,6 +96,12 @@ lazy('engine-cache', 'Engines'); | ||
utils.tryRequire = function tryRequire(fp) { | ||
utils.tryRequire = function tryRequire(fp, opts) { | ||
try { | ||
return require(path.resolve(fp)); | ||
} catch(err) {} | ||
return require(fp); | ||
} catch(err) { | ||
try { | ||
opts = opts || {}; | ||
fp = path.resolve(fp); | ||
return require(fp); | ||
} catch(err) {} | ||
} | ||
return null; | ||
@@ -317,3 +324,3 @@ }; | ||
if (typeof fn !== 'function') { | ||
if (this.option && typeof fn !== 'function') { | ||
fn = this.option('renameKey'); | ||
@@ -338,3 +345,3 @@ } | ||
utils.option = function option(app) { | ||
utils.option = function option(app, prop) { | ||
utils.define(app, 'option', function(key, value) { | ||
@@ -341,0 +348,0 @@ if (typeof key === 'string') { |
@@ -26,9 +26,9 @@ 'use strict'; | ||
addView: function(key, value) { | ||
view: function (key, value) { | ||
if (typeof value !== 'object' && typeof key === 'string') { | ||
return this.addView(this.renameKey(key), {path: key}); | ||
return this.view(this.renameKey(key), {path: key}); | ||
} | ||
if (utils.isObject(key) && key.path) { | ||
return this.addView(key.path, key); | ||
return this.view(key.path, key); | ||
} | ||
@@ -52,4 +52,7 @@ | ||
view.key = this.renameKey(view.key || key); | ||
view = this.extendView(view); | ||
return this.extendView(view); | ||
}, | ||
addView: function(key, value) { | ||
var view = this.view(key, value); | ||
this.emit('view', view.key, view); | ||
@@ -56,0 +59,0 @@ this.views[view.key] = view; |
{ | ||
"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.12", | ||
"version": "0.1.13", | ||
"homepage": "https://github.com/jonschlinkert/templates", | ||
@@ -43,2 +43,3 @@ "author": "Jon Schlinkert (https://github.com/jonschlinkert)", | ||
"lazy-cache": "^0.2.3", | ||
"load-helpers": "^0.2.3", | ||
"micromatch": "^2.2.0", | ||
@@ -45,0 +46,0 @@ "mixin-deep": "^1.1.3", |
@@ -36,12 +36,35 @@ # templates [![NPM version](https://badge.fury.io/js/templates.svg)](http://badge.fury.io/js/templates) | ||
### [.create](index.js#L153) | ||
### [.collection](index.js#L151) | ||
Create a view collection. View collections are stored on the `app.views` object. For example, if you create a collection named `posts`, the all `posts` will be stored on `app.views.posts`. | ||
Create a new view collection. View collections are decorated with special methods for getting, setting and rendering views from that collection. Collections created with this method are not stored on `app.views` as with the [create](#create) method. | ||
**Params** | ||
* `opts` **{Object}**: Collection options | ||
* `returns` **{Object}**: Returns the `collection` instance for chaining. | ||
**Example** | ||
```js | ||
var collection = app.collection(); | ||
collection.addViews({...}); // add an object of views | ||
collection.addView('foo', {content: '...'}); // add a single view | ||
// collection methods are chainable too | ||
collection.addView('home.hbs', {content: 'foo <%= title %> bar'}) | ||
.render({title: 'Home'}, function(err, res) { | ||
//=> 'foo Home bar' | ||
}); | ||
``` | ||
### [.create](index.js#L194) | ||
Create a new view collection that is stored on the `app.views` object. For example, if you create a collection named `posts`, then all `posts` will be stored on `app.views.posts`, and a `posts` method will be added to `app`, allowing you to add posts to the collection using `app.posts()`. | ||
**Params** | ||
* `name` **{String}**: The name of the collection. Plural or singular form. | ||
* `opts` **{Object}**: Collection options | ||
* `loaders` **{String|Array|Function}**: Loaders to use for adding views to the created collection. | ||
* `returns` **{Object}**: Returns the `Template` instance for chaining. | ||
* `returns` **{Object}**: Returns the `collection` instance for chaining. | ||
@@ -62,3 +85,3 @@ **Example** | ||
### [.find](index.js#L300) | ||
### [.find](index.js#L329) | ||
@@ -82,3 +105,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#L338) | ||
### [.getView](index.js#L367) | ||
@@ -105,3 +128,3 @@ Get view `key` from the specified `collection`. | ||
### [.getViews](index.js#L377) | ||
### [.getViews](index.js#L406) | ||
@@ -125,3 +148,3 @@ Get all views from a `collection` using the collection's singular or plural name. | ||
### [.matchView](index.js#L409) | ||
### [.matchView](index.js#L438) | ||
@@ -147,3 +170,3 @@ Returns the first view from `collection` with a key that matches the given glob pattern. | ||
### [.matchViews](index.js#L437) | ||
### [.matchViews](index.js#L466) | ||
@@ -169,3 +192,3 @@ Returns any views from the specified collection with keys that match the given glob pattern. | ||
### [.handle](index.js#L479) | ||
### [.handle](index.js#L508) | ||
@@ -208,3 +231,3 @@ Handle a middleware `method` for `view`. | ||
### [.all](index.js#L589) | ||
### [.all](index.js#L618) | ||
@@ -228,3 +251,3 @@ Special route method that works just like the `router.METHOD()` methods, except that it matches all verbs. | ||
### [.param](index.js#L618) | ||
### [.param](index.js#L647) | ||
@@ -273,3 +296,3 @@ Add callback triggers to route parameters, where `name` is the name of the parameter and `fn` is the callback function. | ||
### [.compile](index.js#L784) | ||
### [.compile](index.js#L813) | ||
@@ -299,3 +322,3 @@ Compile `content` with the given `locals`. | ||
### [.render](index.js#L843) | ||
### [.render](index.js#L872) | ||
@@ -354,2 +377,2 @@ Render a view with the given `locals` and `callback`. | ||
_This file was generated by [verb-cli](https://github.com/assemble/verb-cli) on September 10, 2015._ | ||
_This file was generated by [verb-cli](https://github.com/assemble/verb-cli) on September 11, 2015._ |
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
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
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
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
68457
1934
366
26
3
1
+ Addedload-helpers@^0.2.3
+ Addedasync-array-reduce@0.2.1(transitive)
+ Addedexpand-tilde@1.2.2(transitive)
+ Addedfs-exists-sync@0.1.0(transitive)
+ Addedfs.realpath@1.0.0(transitive)
+ Addedglob@7.2.3(transitive)
+ Addedglobal-modules@0.2.3(transitive)
+ Addedglobal-prefix@0.1.5(transitive)
+ Addedhomedir-polyfill@1.0.3(transitive)
+ Addedini@1.3.8(transitive)
+ Addedis-windows@0.2.0(transitive)
+ Addedisexe@2.0.0(transitive)
+ Addedload-helpers@0.2.11(transitive)
+ Addedmatched@0.4.4(transitive)
+ Addedos-homedir@1.0.2(transitive)
+ Addedparse-passwd@1.0.0(transitive)
+ Addedresolve-dir@0.1.1(transitive)
+ Addedwhich@1.3.1(transitive)