co-nested-hbs
Advanced tools
Comparing version 0.0.2 to 0.1.0
81
index.js
var fs = require('fs'); | ||
var path = require('path'); | ||
var extend = require('xtend'); | ||
var glob = require('glob'); | ||
// thunk! | ||
var handlebars = require('handlebars').create(); | ||
function read(filePath) { | ||
@@ -13,3 +13,2 @@ return function(done) { | ||
// thunk! | ||
function findPartialTemplateFiles(partialsPath) { | ||
@@ -21,3 +20,3 @@ return function(done) { | ||
function* registerPartials(partialsPath, handlebars) { | ||
function* registerPartials(partialsPath) { | ||
var i, len, files, filePath, partialName, rawTemplate; | ||
@@ -36,2 +35,24 @@ | ||
var hbsCache = {}; | ||
var registeredPartials = false; | ||
function *renderTemplate(viewPath, partialsPath, tmpl, locals) { | ||
if (!registeredPartials) { | ||
yield registerPartials(partialsPath); | ||
registeredPartials = true; | ||
} | ||
if (!tmpl.endsWith('.hbs')) { | ||
tmpl = tmpl + '.hbs'; | ||
} | ||
if(!hbsCache[tmpl]) { | ||
var rawTemplate = yield read(path.join(viewPath, tmpl)); | ||
hbsCache[tmpl] = handlebars.compile(rawTemplate); | ||
} | ||
return hbsCache[tmpl](locals); | ||
} | ||
module.exports = function(viewPath, opts) { | ||
@@ -41,3 +62,7 @@ opts = opts || {}; | ||
opts.cache = opts.cache || true; | ||
opts.layouts = opts.layouts || []; | ||
if (opts.layout) { | ||
opts.layouts = [opts.layout]; | ||
} else { | ||
opts.layouts = opts.layouts || []; | ||
} | ||
@@ -48,46 +73,28 @@ if(!(opts.layouts instanceof Array)) { | ||
var hbs = {layouts: opts.layouts, registeredPartials: false}; | ||
var hbs = {layouts: opts.layouts}; | ||
hbs.handlebars = require('handlebars').create(); | ||
hbs.registerHelper = function() { | ||
hbs.handlebars.registerHelper.apply(hbs.handlebars, arguments); | ||
handlebars.registerHelper.apply(handlebars, arguments); | ||
}; | ||
hbs.cache = {}; | ||
hbs.render = function* () { | ||
var buffer=null, templates, locals, i, len, tmpl; | ||
hbs.render = function *(tmpl, locals) { | ||
if (!hbs.registeredPartials) { | ||
yield registerPartials(opts.partialsPath, hbs.handlebars); | ||
hbs.registeredPartials = true; | ||
} | ||
locals = arguments[arguments.length - 1]; | ||
if (!tmpl.endsWith('.hbs')) { | ||
tmpl = tmpl + '.hbs'; | ||
if (typeof locals === 'object') { | ||
templates = Array.prototype.slice.call(arguments, 0, -1); | ||
} else { | ||
locals = {}; | ||
templates = Array.prototype.slice.call(arguments); | ||
} | ||
locals = locals || {}; | ||
if(!hbs.cache[tmpl]) { | ||
var rawTemplate = yield read(path.join(viewPath, tmpl)); | ||
hbs.cache[tmpl] = hbs.handlebars.compile(rawTemplate); | ||
} | ||
templates = templates.concat(hbs.layouts); | ||
return hbs.cache[tmpl](locals); | ||
}; | ||
for(i = 0, len = templates.length; i < len; i++) { | ||
tmpl = templates[i]; | ||
hbs.renderAll = function *renderAll (arr, globalLocals) { | ||
var buffer=null, | ||
i, len, tmpl, locals; | ||
globalLocals = globalLocals || {}; | ||
arr = arr.concat(hbs.layouts); | ||
for(i = 0, len = arr.length; i < len; i++) { | ||
tmpl = arr[i][0]; | ||
locals = arr[i][1] || {}; | ||
locals.body = buffer; | ||
buffer = yield hbs.render(tmpl, extend(globalLocals, locals)); | ||
buffer = yield renderTemplate(viewPath, opts.partialsPath, tmpl, locals); | ||
} | ||
@@ -94,0 +101,0 @@ |
{ | ||
"name": "co-nested-hbs", | ||
"version": "0.0.2", | ||
"version": "0.1.0", | ||
"repository": "speedmanly/co-nested-hbs", | ||
@@ -15,4 +15,3 @@ "description": "Generator-based Handlebars templates for nested layouts.", | ||
"handlebars": "~1.3", | ||
"glob": "~3.2", | ||
"xtend": "~2.1" | ||
"glob": "~3.2" | ||
}, | ||
@@ -19,0 +18,0 @@ "devDependencies": { |
@@ -53,7 +53,3 @@ # co-nested-hbs | ||
co(function *() { | ||
var html = yield view.renderAll([ | ||
['home', {name: 'Bob'}], | ||
['simple_theme'], | ||
['overall_layout', {title: 'Hello World!'}] | ||
]); | ||
var html = yield view.render('home', 'simple_theme', 'overall_layout', {title: 'Hello World!'}); | ||
})(); | ||
@@ -90,3 +86,3 @@ ``` | ||
var view = require('co-nested-hbs')('view_path/goes/here', { | ||
layout: 'layout_file', // specify implied layout (or layouts) to be added to each renderAll() call. | ||
layout: 'layout_file', // specify implied layout (or layouts) to be added to each render() call. | ||
partialsPath: 'path_to_partials', | ||
@@ -105,14 +101,6 @@ cache: true | ||
// render the following templates in a chain, building the 'nest' | ||
var html = yield view.renderAll([ | ||
['first_render', {local: 'variable'}], | ||
['second_render'], // {{{body}}} here will refer to the first_render html output | ||
['third_render', {title: 'Hello World!'}] // {{{body}}} here will refer to the second_render html output. voila, nesting. | ||
]); | ||
var html = yield view.render('first_render', 'second_render', 'third_render'); | ||
// 'global' locals can be applied to all templates when they are rendered | ||
var html = yield view.renderAll([ | ||
['first_render', {local: 'variable'}], | ||
['second_render'], | ||
['third_render', {title: 'Hello World!'}] | ||
], {global_local: 'applied to all templates'}); | ||
// locals can be applied to all templates when they are rendered | ||
var html = yield view.render('first_render', 'second_render', 'third_render', {title: 'Hello World!'}, {global_local: 'applied to all templates'}); | ||
} | ||
@@ -119,0 +107,0 @@ ``` |
@@ -9,54 +9,25 @@ var assert = require('assert'); | ||
describe('view.render', function() { | ||
it('returns html with locals filled in', function(done) { | ||
it('nests output', function(done) { | ||
co(function *() { | ||
var view = require('..')('test'), | ||
html = yield view.render('locals', {world: 'world'}); | ||
html = yield view.render('c', 'b', 'a'); | ||
assert.equal(html, 'hello world\n'); | ||
assert.equal(html, '<a><b>c\n</b>\n</a>\n'); | ||
})(done); | ||
}); | ||
it('returns html with partials', function(done) { | ||
it('a layout can be specified', function(done) { | ||
co(function *() { | ||
var view = require('..')('test'); | ||
var view = require('..')('test', {layout: 'a'}), | ||
html = yield view.render('c'); | ||
var html = yield view.render('partial', {world: 'world'}); | ||
assert.equal(html, 'hello partial world\n\n'); | ||
assert.equal(html, '<a>c\n</a>\n'); | ||
})(done); | ||
}); | ||
it('returns html with a helper', function(done) { | ||
it('multiple layouts can be specified', function(done) { | ||
co(function *() { | ||
var html, | ||
view = require('..')('test'); | ||
var view = require('..')('test', {layouts: ['b', 'a']}), | ||
html = yield view.render('c'); | ||
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); | ||
}); | ||
it('caches templates', function(done) { | ||
co(function *() { | ||
var view = require('..')('test'); | ||
yield view.render('a'); | ||
assert(view.cache['a.hbs'], 'template is cached'); | ||
})(done); | ||
}); | ||
}); | ||
describe('view.renderAll', function() { | ||
it('nests output', function(done) { | ||
co(function *() { | ||
var view = require('..')('test'), | ||
html = yield view.renderAll([ | ||
['c'], | ||
['b'], | ||
['a'] | ||
]); | ||
assert.equal(html, '<a><b>c\n</b>\n</a>\n'); | ||
@@ -66,57 +37,34 @@ })(done); | ||
it('each template can have locals', function(done) { | ||
it('accepts locals which are then applied to every template', function(done) { | ||
co(function *() { | ||
var view = require('..')('test'), | ||
html = yield view.renderAll([ | ||
['locals', {world: 'world'}], | ||
['a'] | ||
]); | ||
html = yield view.render('locals', {world: 'world'}); | ||
assert.equal(html, '<a>hello world\n</a>\n'); | ||
assert.equal(html, 'hello world\n'); | ||
})(done); | ||
}); | ||
it('a layout can be specified', function(done) { | ||
it('finds and renders partials', function(done) { | ||
co(function *() { | ||
var view = require('..')('test', {layouts: 'a'}), | ||
html = yield view.renderAll([ | ||
['locals', {world: 'world'}] | ||
]); | ||
var view = require('..')('test'); | ||
assert.equal(html, '<a>hello world\n</a>\n'); | ||
})(done); | ||
}); | ||
var html = yield view.render('partial', {world: 'world'}); | ||
it('multiple layouts can be specified', function(done) { | ||
co(function *() { | ||
var view = require('..')('test', {layouts: ['b', 'a']}), | ||
html = yield view.renderAll([ | ||
['c'] | ||
]); | ||
assert.equal(html, '<a><b>c\n</b>\n</a>\n'); | ||
assert.equal(html, 'hello partial world\n\n'); | ||
})(done); | ||
}); | ||
it('accepts global locals applied to every template', function(done) { | ||
it('registers and uses helpers', function(done) { | ||
co(function *() { | ||
var view = require('..')('test'), | ||
html = yield view.renderAll([ | ||
['locals'] | ||
], {world: 'world'}); | ||
var html, | ||
view = require('..')('test'); | ||
assert.equal(html, 'hello world\n'); | ||
})(done); | ||
}); | ||
view.registerHelper('link-to', function(text, url) { | ||
return '<a href="' + url + '">' + text + '</a>'; | ||
}); | ||
it('locals override global locals', function(done) { | ||
co(function *() { | ||
var view = require('..')('test'), | ||
html = yield view.renderAll([ | ||
['locals', {world: 'worldy'}] | ||
], {world: 'world'}); | ||
assert.equal(html, 'hello worldy\n'); | ||
html = yield view.render('helper'); | ||
assert.equal(html, 'hello <a href="http://www.world.com/">world</a>\n'); | ||
})(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
2
7118
127
109
- Removedxtend@~2.1
- Removedobject-keys@0.4.0(transitive)
- Removedxtend@2.1.2(transitive)