engine-cache
Advanced tools
Comparing version 0.10.2 to 0.10.3
98
index.js
@@ -6,3 +6,2 @@ 'use strict'; | ||
var Helpers = require('helper-cache'); | ||
var slice = require('array-slice'); | ||
var extend = require('extend-shallow'); | ||
@@ -39,3 +38,3 @@ var forOwn = require('for-own'); | ||
Engines.prototype.init = function(engines) { | ||
debug('init', arguments); | ||
debug('init %j', arguments); | ||
this.cache = engines || {}; | ||
@@ -60,8 +59,6 @@ }; | ||
Engines.prototype.setEngine = function (ext, fn, options) { | ||
var args = slice(arguments); | ||
debug('[set]', arguments); | ||
debug('[set]: %s, %j, %j', ext, fn, options); | ||
var engine = {}; | ||
if (args.length === 3) { | ||
if (arguments.length === 3) { | ||
if (options && (typeof options === 'function' || | ||
@@ -130,6 +127,3 @@ options.hasOwnProperty('render') || | ||
Engines.prototype.getEngine = function(ext) { | ||
if (!ext) { | ||
return this.cache; | ||
} | ||
if (!ext) return this.cache; | ||
if (ext[0] !== '.') { | ||
@@ -161,22 +155,19 @@ ext = '.' + ext; | ||
Engines.prototype.decorate = function(engine) { | ||
debug('[decorate]', arguments); | ||
debug('[decorate]: %j', engine); | ||
var renderSync = engine.renderSync; | ||
var render = engine.render; | ||
var compile = engine.compile || function (str, options) { | ||
var compile = engine.compile || function (str) { | ||
return str; | ||
}; | ||
engine.compile = function (str, options) { | ||
if (typeof str === 'function') { | ||
return str; | ||
} | ||
var opts = extend({}, options); | ||
var res = compile(str, mergeHelpers.call(this, opts)); | ||
return res; | ||
} | ||
engine.compile = function (str, opts) { | ||
if (typeof str === 'function') return str; | ||
opts = opts || {}; | ||
return compile(str, mergeHelpers.call(this, opts)); | ||
}; | ||
engine.render = function(str, options, callback) { | ||
engine.render = function(str, options, cb) { | ||
if (typeof options === 'function') { | ||
callback = options; | ||
cb = options; | ||
options = {}; | ||
@@ -189,22 +180,22 @@ } | ||
if (typeof str === 'function') { | ||
return this.resolve(str(options), callback); | ||
return this.resolve(str(options), cb); | ||
} | ||
var opts = extend({async: true}, options); | ||
var opts = extend({}, {async: true}, options); | ||
var self = this; | ||
return render.call(this, str, mergeHelpers.call(this, opts), function (err, content) { | ||
if (err) return callback(err); | ||
return self.resolve(content, callback); | ||
if (err) return cb(err); | ||
return self.resolve(content, cb); | ||
}); | ||
}; | ||
engine.renderSync = function(str, options) { | ||
engine.renderSync = function(str, opts) { | ||
if (typeof str !== 'function') { | ||
str = this.compile(str, options); | ||
str = this.compile(str, opts); | ||
} | ||
if (typeof str === 'function') { | ||
return str(options); | ||
return str(opts); | ||
} | ||
var opts = options || {}; | ||
opts = opts || {}; | ||
opts.helpers = extend({}, this.helpers, opts.helpers); | ||
@@ -214,3 +205,3 @@ return renderSync(str, opts); | ||
engine.resolve = function (str, callback) { | ||
engine.resolve = function (str, cb) { | ||
var self = this; | ||
@@ -231,7 +222,6 @@ // `stash` contains the objects created when rendering the template | ||
}, function (err) { | ||
if (err) return callback(err); | ||
callback(null, str); | ||
if (err) return cb(err); | ||
cb(null, str); | ||
}); | ||
}; | ||
}; | ||
@@ -253,14 +243,11 @@ | ||
Engines.prototype.load = function(obj) { | ||
debug('[load]', arguments); | ||
Engines.prototype.load = function(engines) { | ||
debug('[load]: %j', engines); | ||
var engines = Object.keys(obj); | ||
var len = engines.length; | ||
var i = 0; | ||
while (i < len) { | ||
var name = engines[i++]; | ||
var engine = obj[name]; | ||
if (name !== 'clearCache') { | ||
this.setEngine(name, engine); | ||
for (var key in engines) { | ||
if (engines.hasOwnProperty(key)) { | ||
var engine = engines[key]; | ||
if (key !== 'clearCache') { | ||
this.setEngine(key, engine); | ||
} | ||
} | ||
@@ -311,5 +298,3 @@ } | ||
if (ext) { | ||
if (ext[0] !== '.') { | ||
ext = '.' + ext; | ||
} | ||
if (ext[0] !== '.') ext = '.' + ext; | ||
delete this.cache[ext]; | ||
@@ -329,11 +314,9 @@ } else { | ||
function filterHelpers (helpers, async) { | ||
function filterHelpers(helpers, async) { | ||
var res = {}; | ||
var keys = Object.keys(helpers || {}); | ||
var len = keys.length; | ||
var i = 0; | ||
while (len--) { | ||
var key = keys[i++]; | ||
if (helpers[key].async == async) { | ||
res[key] = helpers[key]; | ||
for (var key in helpers) { | ||
if (helpers.hasOwnProperty(key)) { | ||
if (helpers[key].async == async) { | ||
res[key] = helpers[key]; | ||
} | ||
} | ||
@@ -360,5 +343,4 @@ } | ||
filterHelpers(options.helpers), | ||
this.asyncHelpers.get({wrap: options.async})); | ||
this.asyncHelpers.get({wrap: !!options.async})); | ||
return options; | ||
} |
{ | ||
"name": "engine-cache", | ||
"description": "express.js inspired template-engine manager.", | ||
"version": "0.10.2", | ||
"version": "0.10.3", | ||
"homepage": "https://github.com/jonschlinkert/engine-cache", | ||
@@ -16,2 +16,8 @@ "author": { | ||
], | ||
"contributors": [ | ||
{ | ||
"name": "Brian Woodward", | ||
"url": "https://github.com/doowb" | ||
} | ||
], | ||
"repository": { | ||
@@ -39,17 +45,16 @@ "type": "git", | ||
"dependencies": { | ||
"array-slice": "^0.2.2", | ||
"async": "^0.9.0", | ||
"async-helpers": "^0.1.1", | ||
"debug": "^2.1.1", | ||
"extend-shallow": "^0.2.0", | ||
"for-own": "^0.1.2", | ||
"async-helpers": "^0.1.2", | ||
"debug": "^2.1.3", | ||
"extend-shallow": "^1.1.2", | ||
"for-own": "^0.1.3", | ||
"helper-cache": "^0.6.0" | ||
}, | ||
"devDependencies": { | ||
"consolidate": "^0.11.0", | ||
"engine-handlebars": "^0.4.1", | ||
"consolidate": "^0.12.1", | ||
"engine-handlebars": "^0.6.1", | ||
"engine-lodash": "^0.5.0", | ||
"engines": "^0.4.0", | ||
"handlebars": "^3.0.0", | ||
"lodash": "^3.2.0", | ||
"handlebars": "^3.0.1", | ||
"lodash": "^3.6.0", | ||
"mocha": "^2.2.1", | ||
@@ -56,0 +61,0 @@ "should": "^5.2.0", |
@@ -18,3 +18,3 @@ # engine-cache [![NPM version](https://badge.fury.io/js/engine-cache.svg)](http://badge.fury.io/js/engine-cache) | ||
## API | ||
### [Engines](./index.js#L27) | ||
### [Engines](./index.js#L26) | ||
@@ -28,3 +28,3 @@ * `engines` **{Object}**: Optionally pass an object of engines to initialize with. | ||
### [.setEngine](./index.js#L57) | ||
### [.setEngine](./index.js#L56) | ||
@@ -43,3 +43,3 @@ Register the given view engine callback `fn` as `ext`. | ||
### [.getEngine](./index.js#L126) | ||
### [.getEngine](./index.js#L123) | ||
@@ -59,3 +59,3 @@ Return the engine stored by `ext`. If no `ext` is passed, the entire cache is returned. | ||
### [.load](./index.js#L245) | ||
### [.load](./index.js#L235) | ||
@@ -71,3 +71,3 @@ Load an object of engines onto the `cache`. Mostly useful for testing, but exposed as a public method. | ||
### [.helpers](./index.js#L282) | ||
### [.helpers](./index.js#L269) | ||
@@ -90,3 +90,3 @@ Get and set helpers for the given `ext` (engine). If no `ext` is passed, the entire helper cache is returned. | ||
### [.clear](./index.js#L300) | ||
### [.clear](./index.js#L287) | ||
@@ -103,7 +103,23 @@ Remove `ext` engine from the cache, or if no value is specified the entire cache is reset. | ||
## Related | ||
* [helper-cache](https://github.com/jonschlinkert/helper-cache): Easily register and get helper functions to be passed to any template engine or node.js application. Methods for both sync and async helpers. | ||
* [async-helpers](https://github.com/doowb/async-helpers): Use async helpers in template engines like Handlebars and Lodash. | ||
* [template](https://github.com/jonschlinkert/template): Render templates from any engine. Make custom template types, use layouts on pages, partials or any custom template type, custom delimiters, helpers, middleware, routes, loaders, and lots more. Powers Assemble v0.6.0, Verb v0.3.0 and your application. | ||
* [template-helpers](https://github.com/jonschlinkert/template-helpers): Generic JavaScript helpers that can be used with any template engine. Handlebars, Lo-Dash, Underscore, or any engine that supports helper functions. | ||
* [handlebars-helpers](https://github.com/assemble/handlebars-helpers): 120+ Handlebars helpers in ~20 categories, for Assemble, YUI, Ghost or any Handlebars project. Includes helpers like {{i18}}, {{markdown}}, {{relative}}, {{extend}}, {{moment}}, and so on. | ||
## Running tests | ||
Install dev dependencies: | ||
```bash | ||
npm i -d && npm test | ||
``` | ||
## Contributing | ||
Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](https://github.com/jonschlinkert/engine-cache/issues) | ||
## Author | ||
**Jon Schlinkert** | ||
+ [github/jonschlinkert](https://github.com/jonschlinkert) | ||
@@ -120,2 +136,3 @@ + [twitter/jonschlinkert](http://twitter.com/jonschlinkert) | ||
[helper-cache]: https://github.com/jonschlinkert/helper-cache | ||
[helper-cache]: https://github.com/jonschlinkert/helper-cache | ||
<!-- deps: swig lodash mocha engine-lodash handlebars --> |
15091
6
131
292
+ Addedextend-shallow@1.1.4(transitive)
+ Addedkind-of@1.1.0(transitive)
- Removedarray-slice@^0.2.2
- Removedarray-slice@0.2.3(transitive)
- Removedextend-shallow@0.2.0(transitive)
Updatedasync-helpers@^0.1.2
Updateddebug@^2.1.3
Updatedextend-shallow@^1.1.2
Updatedfor-own@^0.1.3