co-nested-hbs
Advanced tools
Comparing version 1.0.0 to 2.0.0
59
index.js
@@ -1,26 +0,22 @@ | ||
var fs = require('fs'); | ||
var fs = require('fs').promises; | ||
var path = require('path'); | ||
var glob = require('glob'); | ||
var globby = require('globby'); | ||
var Handlebars = require('handlebars'); | ||
function read(filePath) { | ||
return function(done) { | ||
fs.readFile(filePath, {encoding: 'utf8'}, done); | ||
}; | ||
async function read(filePath) { | ||
return await fs.readFile(filePath, { encoding: 'utf8' }); | ||
} | ||
function findPartialTemplateFiles(partialsPath) { | ||
return function(done) { | ||
glob(path.join(partialsPath, '**/_*.hbs'), done); | ||
}; | ||
async function findPartialTemplateFiles(partialsPath) { | ||
return await globby(path.join(partialsPath, '**/_*.hbs')); | ||
} | ||
function *registerPartials(handlebars, partialsPath) { | ||
async function registerPartials(handlebars, partialsPath) { | ||
var i, len, files, filePath, partialName, rawTemplate; | ||
files = yield findPartialTemplateFiles(partialsPath); | ||
files = await findPartialTemplateFiles(partialsPath); | ||
for(i = 0, len = files.length; i < len; i++) { | ||
for (i = 0, len = files.length; i < len; i++) { | ||
filePath = files[i]; | ||
rawTemplate = yield read(filePath); | ||
rawTemplate = await read(filePath); | ||
partialName = path.basename(filePath, '.hbs').substring(1); | ||
@@ -35,3 +31,3 @@ handlebars.registerPartial(partialName, rawTemplate); | ||
function *renderTemplate(handlebars, tmpl, locals) { | ||
async function renderTemplate(handlebars, tmpl, locals) { | ||
if (!tmpl.endsWith('.hbs')) { | ||
@@ -41,4 +37,4 @@ tmpl = tmpl + '.hbs'; | ||
if(!hbsCache[tmpl]) { | ||
var rawTemplate = yield read(tmpl); | ||
if (!hbsCache[tmpl]) { | ||
var rawTemplate = await read(tmpl); | ||
hbsCache[tmpl] = handlebars.compile(rawTemplate); | ||
@@ -50,3 +46,3 @@ } | ||
module.exports = function(viewPath, opts) { | ||
module.exports = function (viewPath, opts) { | ||
var handlebars = Handlebars.create(); | ||
@@ -63,18 +59,25 @@ | ||
if(!(opts.layouts instanceof Array)) { | ||
if (!(opts.layouts instanceof Array)) { | ||
opts.layouts = [opts.layouts]; | ||
} | ||
var hbs = {layouts: opts.layouts, handlebars: handlebars}; | ||
var hbs = { layouts: opts.layouts, handlebars: handlebars }; | ||
hbs.registerHelper = function() { | ||
hbs.registerHelper = function () { | ||
handlebars.registerHelper.apply(handlebars, arguments); | ||
}; | ||
hbs.render = function* () { | ||
var buffer=null, lastArg, secondToLastArg, | ||
renderOpts, locals, templates, i, len, tmpl; | ||
hbs.render = async function () { | ||
var buffer = null, | ||
lastArg, | ||
secondToLastArg, | ||
renderOpts, | ||
locals, | ||
templates, | ||
i, | ||
len, | ||
tmpl; | ||
if (opts.partialsPath && !registeredPartialPaths[opts.partialsPath]) { | ||
yield registerPartials(handlebars, opts.partialsPath); | ||
await registerPartials(handlebars, opts.partialsPath); | ||
registeredPartialPaths[opts.partialsPath] = true; | ||
@@ -103,3 +106,3 @@ } | ||
templates = templates.map(function(tmpl) { | ||
templates = templates.map(function (tmpl) { | ||
return path.join(viewPath, tmpl); | ||
@@ -116,3 +119,3 @@ }); | ||
for(i = 0, len = templates.length; i < len; i++) { | ||
for (i = 0, len = templates.length; i < len; i++) { | ||
tmpl = templates[i]; | ||
@@ -122,3 +125,3 @@ | ||
buffer = yield renderTemplate(handlebars, tmpl, locals); | ||
buffer = await renderTemplate(handlebars, tmpl, locals); | ||
} | ||
@@ -125,0 +128,0 @@ |
{ | ||
"name": "co-nested-hbs", | ||
"version": "1.0.0", | ||
"repository": "speedmanly/co-nested-hbs", | ||
"version": "2.0.0", | ||
"repository": "chrisvariety/co-nested-hbs", | ||
"description": "Generator-based Handlebars templates for nested layouts.", | ||
@@ -14,7 +14,6 @@ "keywords": [ | ||
"dependencies": { | ||
"handlebars": "~1.3", | ||
"glob": "~3.2" | ||
"globby": "^11.0.4", | ||
"handlebars": "^4.7.7" | ||
}, | ||
"devDependencies": { | ||
"co": "~3.0", | ||
"mocha": "*", | ||
@@ -21,0 +20,0 @@ "should": "*" |
@@ -46,11 +46,7 @@ # co-nested-hbs | ||
```js | ||
var r = require('rethinkdb'); | ||
var co = require('co'); | ||
var view = require('co-nested-hbs'); | ||
co(function *() { | ||
var html = yield view.render('home', 'simple_theme', 'overall_layout', {title: 'Hello World!'}); | ||
})(); | ||
var html = await view.render('home', 'simple_theme', 'overall_layout', { title: 'Hello World!' }); | ||
``` | ||
@@ -109,2 +105,2 @@ | ||
MIT | ||
MIT |
@@ -6,113 +6,90 @@ var assert = require('assert'); | ||
// preload | ||
require('..')('test') | ||
require('..')('test'); | ||
it('allows for different partials with the same name across different partialPaths', function(done) { | ||
co(function *() { | ||
var view1 = require('..')('test'); | ||
var view2 = require('..')('test_instances'); | ||
it('allows for different partials with the same name across different partialPaths', async function () { | ||
var view1 = require('..')('test'); | ||
var view2 = require('..')('test_instances'); | ||
var html1 = yield view1.render('partial', {world: 'world'}); | ||
assert.equal(html1, 'hello partial world\n\n'); | ||
var html2 = yield view2.render('partial'); | ||
assert.equal(html2, 'hello partial with different view path\n\n'); | ||
})(done); | ||
var html1 = await view1.render('partial', { world: 'world' }); | ||
assert.equal(html1, 'hello partial world\n'); | ||
var html2 = await view2.render('partial'); | ||
assert.equal(html2, 'hello partial with different view path\n'); | ||
}); | ||
describe('view.render', function() { | ||
it('nests output', function(done) { | ||
co(function *() { | ||
var view = require('..')('test'), | ||
html = yield view.render('c', 'b', 'a'); | ||
describe('view.render', function () { | ||
it('nests output', async function () { | ||
var view = require('..')('test'), | ||
html = await view.render('c', 'b', 'a'); | ||
assert.equal(html, '<a><b>c\n</b>\n</a>\n'); | ||
})(done); | ||
assert.equal(html, '<a><b>c\n</b>\n</a>\n'); | ||
}); | ||
it('a layout can be specified', function(done) { | ||
co(function *() { | ||
var view = require('..')('test', {layout: 'test/a'}), | ||
html = yield view.render('c'); | ||
it('a layout can be specified', async function () { | ||
var view = require('..')('test', { layout: 'test/a' }), | ||
html = await view.render('c'); | ||
assert.equal(html, '<a>c\n</a>\n'); | ||
})(done); | ||
assert.equal(html, '<a>c\n</a>\n'); | ||
}); | ||
it('multiple layouts can be specified', function(done) { | ||
co(function *() { | ||
var view = require('..')('test', {layouts: ['test/b', 'test/a']}), | ||
html = yield view.render('c'); | ||
it('multiple layouts can be specified', async function () { | ||
var view = require('..')('test', { layouts: ['test/b', 'test/a'] }), | ||
html = await view.render('c'); | ||
assert.equal(html, '<a><b>c\n</b>\n</a>\n'); | ||
})(done); | ||
assert.equal(html, '<a><b>c\n</b>\n</a>\n'); | ||
}); | ||
it('accepts locals which are then applied to every template', function(done) { | ||
co(function *() { | ||
var view = require('..')('test'), | ||
html = yield view.render('locals', {world: 'world'}); | ||
it('accepts locals which are then applied to every template', async function () { | ||
var view = require('..')('test'), | ||
html = await view.render('locals', { world: 'world' }); | ||
assert.equal(html, 'hello world\n'); | ||
})(done); | ||
assert.equal(html, 'hello world\n'); | ||
}); | ||
it('accepts locals and render options', function(done) { | ||
co(function *() { | ||
var view = require('..')('test'), | ||
html = yield view.render('locals', {world: 'world'}, {layout: false}); | ||
it('accepts locals and render options', async function () { | ||
var view = require('..')('test'), | ||
html = await view.render('locals', { world: 'world' }, { layout: false }); | ||
assert.equal(html, 'hello world\n'); | ||
})(done); | ||
assert.equal(html, 'hello world\n'); | ||
}); | ||
it('finds and renders partials', function(done) { | ||
co(function *() { | ||
var view = require('..')('test'); | ||
it('finds and renders partials', async function () { | ||
var view = require('..')('test'); | ||
var html = yield view.render('partial', {world: 'world'}); | ||
var html = await view.render('partial', { world: 'world' }); | ||
assert.equal(html, 'hello partial world\n\n'); | ||
})(done); | ||
assert.equal(html, 'hello partial world\n'); | ||
}); | ||
it('registers and uses helpers', function(done) { | ||
co(function *() { | ||
var html, | ||
view = require('..')('test'); | ||
it('registers and uses helpers', async function () { | ||
var html, | ||
view = require('..')('test'); | ||
view.registerHelper('link-to', function(text, url) { | ||
return '<a href="' + url + '">' + text + '</a>'; | ||
}); | ||
view.registerHelper('link-to', function (text, url) { | ||
return '<a href="' + url + '">' + text + '</a>'; | ||
}); | ||
html = yield view.render('helper'); | ||
assert.equal(html, 'hello <a href="http://www.world.com/">world</a>\n'); | ||
})(done); | ||
html = await view.render('helper'); | ||
assert.equal(html, 'hello <a href="http://www.world.com/">world</a>\n'); | ||
}); | ||
it('takes a last argument to disable the global layouts', function(done) { | ||
co(function *() { | ||
var view = require('..')('test', {layout: 'a'}), | ||
html = yield view.render('c', {}, {layout: false}); | ||
it('takes a last argument to disable the global layouts', async function () { | ||
var view = require('..')('test', { layout: 'a' }), | ||
html = await view.render('c', {}, { layout: false }); | ||
assert.equal(html, 'c\n'); | ||
})(done); | ||
assert.equal(html, 'c\n'); | ||
}); | ||
it('takes a last argument to choose a new layout', function(done) { | ||
co(function *() { | ||
var view = require('..')('test', {layout: 'test/a'}), | ||
html = yield view.render('c', {}, {layout: 'test/b'}); | ||
it('takes a last argument to choose a new layout', async function () { | ||
var view = require('..')('test', { layout: 'test/a' }), | ||
html = await view.render('c', {}, { layout: 'test/b' }); | ||
assert.equal(html, '<b>c\n</b>\n'); | ||
})(done); | ||
assert.equal(html, '<b>c\n</b>\n'); | ||
}); | ||
it('ignores a null layout', function(done) { | ||
co(function *() { | ||
var view = require('..')('test', {layout: 'test/a'}), | ||
html = yield view.render('c', {}, {layout: null}); | ||
it('ignores a null layout', async function () { | ||
var view = require('..')('test', { layout: 'test/a' }), | ||
html = await view.render('c', {}, { layout: null }); | ||
assert.equal(html, '<a>c\n</a>\n'); | ||
})(done); | ||
assert.equal(html, '<a>c\n</a>\n'); | ||
}); | ||
}); |
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
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
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
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
2
9097
15
168
105
1
+ Addedglobby@^11.0.4
+ Added@nodelib/fs.scandir@2.1.5(transitive)
+ Added@nodelib/fs.stat@2.0.5(transitive)
+ Added@nodelib/fs.walk@1.2.8(transitive)
+ Addedarray-union@2.1.0(transitive)
+ Addedbraces@3.0.3(transitive)
+ Addeddir-glob@3.0.1(transitive)
+ Addedfast-glob@3.3.2(transitive)
+ Addedfastq@1.17.1(transitive)
+ Addedfill-range@7.1.1(transitive)
+ Addedglob-parent@5.1.2(transitive)
+ Addedglobby@11.1.0(transitive)
+ Addedhandlebars@4.7.8(transitive)
+ Addedignore@5.3.2(transitive)
+ Addedis-extglob@2.1.1(transitive)
+ Addedis-glob@4.0.3(transitive)
+ Addedis-number@7.0.0(transitive)
+ Addedmerge2@1.4.1(transitive)
+ Addedmicromatch@4.0.8(transitive)
+ Addedminimist@1.2.8(transitive)
+ Addedneo-async@2.6.2(transitive)
+ Addedpath-type@4.0.0(transitive)
+ Addedpicomatch@2.3.1(transitive)
+ Addedqueue-microtask@1.2.3(transitive)
+ Addedreusify@1.0.4(transitive)
+ Addedrun-parallel@1.2.0(transitive)
+ Addedslash@3.0.0(transitive)
+ Addedsource-map@0.6.1(transitive)
+ Addedto-regex-range@5.0.1(transitive)
+ Addeduglify-js@3.19.3(transitive)
+ Addedwordwrap@1.0.0(transitive)
- Removedglob@~3.2
- Removedamdefine@1.0.1(transitive)
- Removedasync@0.2.10(transitive)
- Removedglob@3.2.11(transitive)
- Removedhandlebars@1.3.0(transitive)
- Removedinherits@2.0.4(transitive)
- Removedlru-cache@2.7.3(transitive)
- Removedminimatch@0.3.0(transitive)
- Removedoptimist@0.3.7(transitive)
- Removedsigmund@1.0.1(transitive)
- Removedsource-map@0.1.43(transitive)
- Removeduglify-js@2.3.6(transitive)
- Removedwordwrap@0.0.3(transitive)
Updatedhandlebars@^4.7.7