Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

co-nested-hbs

Package Overview
Dependencies
Maintainers
2
Versions
9
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

co-nested-hbs - npm Package Compare versions

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');
});
});
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc