co-nested-hbs
Advanced tools
Comparing version 0.3.0 to 1.0.0
22
index.js
var fs = require('fs'); | ||
var path = require('path'); | ||
var glob = require('glob'); | ||
var Handlebars = require('handlebars'); | ||
var handlebars = require('handlebars').create(); | ||
function read(filePath) { | ||
@@ -19,3 +18,3 @@ return function(done) { | ||
function* registerPartials(partialsPath) { | ||
function *registerPartials(handlebars, partialsPath) { | ||
var i, len, files, filePath, partialName, rawTemplate; | ||
@@ -29,3 +28,2 @@ | ||
partialName = path.basename(filePath, '.hbs').substring(1); | ||
handlebars.registerPartial(partialName, rawTemplate); | ||
@@ -37,5 +35,5 @@ } | ||
var registeredPartials = false; | ||
var registeredPartialPaths = {}; | ||
function* renderTemplate(tmpl, locals) { | ||
function *renderTemplate(handlebars, tmpl, locals) { | ||
if (!tmpl.endsWith('.hbs')) { | ||
@@ -54,2 +52,4 @@ tmpl = tmpl + '.hbs'; | ||
module.exports = function(viewPath, opts) { | ||
var handlebars = Handlebars.create(); | ||
opts = opts || {}; | ||
@@ -75,8 +75,8 @@ opts.partialsPath = opts.partialsPath || viewPath; | ||
hbs.render = function* () { | ||
var buffer=null, lastArg, secondToLastArg, renderViewPath, | ||
var buffer=null, lastArg, secondToLastArg, | ||
renderOpts, locals, templates, i, len, tmpl; | ||
if (!registeredPartials) { | ||
yield registerPartials(opts.partialsPath); | ||
registeredPartials = true; | ||
if (opts.partialsPath && !registeredPartialPaths[opts.partialsPath]) { | ||
yield registerPartials(handlebars, opts.partialsPath); | ||
registeredPartialPaths[opts.partialsPath] = true; | ||
} | ||
@@ -121,3 +121,3 @@ | ||
buffer = yield renderTemplate(tmpl, locals); | ||
buffer = yield renderTemplate(handlebars, tmpl, locals); | ||
} | ||
@@ -124,0 +124,0 @@ |
{ | ||
"name": "co-nested-hbs", | ||
"version": "0.3.0", | ||
"version": "1.0.0", | ||
"repository": "speedmanly/co-nested-hbs", | ||
@@ -5,0 +5,0 @@ "description": "Generator-based Handlebars templates for nested layouts.", |
@@ -6,4 +6,16 @@ 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'); | ||
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); | ||
}); | ||
describe('view.render', function() { | ||
@@ -10,0 +22,0 @@ it('nests output', function(done) { |
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
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
9543
16
187
1