co-nested-hbs
Advanced tools
Comparing version 0.0.1 to 0.0.2
63
index.js
var fs = require('fs'); | ||
var path = require('path'); | ||
var readdirp = require('readdirp'); | ||
var extend = require('xtend'); | ||
var glob = require('glob'); | ||
// thunk | ||
var read = function (filename) { | ||
// thunk! | ||
function read(filePath) { | ||
return function(done) { | ||
fs.readFile(filename, {encoding: 'utf8'}, done); | ||
fs.readFile(filePath, {encoding: 'utf8'}, done); | ||
}; | ||
}; | ||
} | ||
function registerPartials(partialsPath, handlebars) { | ||
var stream = readdirp({root: partialsPath, fileFilter: '_*.hbs'}); | ||
// thunk! | ||
function findPartialTemplateFiles(partialsPath) { | ||
return function(done) { | ||
glob(path.join(partialsPath, '**/_*.hbs'), done); | ||
}; | ||
} | ||
stream | ||
.on('warn', function (err) { | ||
console.error('non-fatal error', err); | ||
}) | ||
.on('error', function (err) { console.error('fatal error', err); }) | ||
.on('data', function (entry) { | ||
// TODO: how to get rid of this readFileSync? | ||
var rawTemplate = fs.readFileSync(entry.fullPath, {encoding: 'utf8'}); | ||
handlebars.registerPartial(path.basename(entry.name, '.hbs').substring(1), rawTemplate); | ||
}); | ||
function* registerPartials(partialsPath, handlebars) { | ||
var i, len, files, filePath, partialName, rawTemplate; | ||
files = yield findPartialTemplateFiles(partialsPath); | ||
for(i = 0, len = files.length; i < len; i++) { | ||
filePath = files[i]; | ||
rawTemplate = yield read(filePath); | ||
partialName = path.basename(filePath, '.hbs').substring(1); | ||
handlebars.registerPartial(partialName, rawTemplate); | ||
} | ||
} | ||
@@ -31,9 +38,12 @@ | ||
opts.cache = opts.cache || true; | ||
opts.layouts = opts.layouts || []; | ||
var hbs = {}; | ||
if(!(opts.layouts instanceof Array)) { | ||
opts.layouts = [opts.layouts]; | ||
} | ||
var hbs = {layouts: opts.layouts, registeredPartials: false}; | ||
hbs.handlebars = require('handlebars').create(); | ||
registerPartials(opts.partialsPath, hbs.handlebars); | ||
hbs.registerHelper = function() { | ||
@@ -46,2 +56,7 @@ hbs.handlebars.registerHelper.apply(hbs.handlebars, arguments); | ||
hbs.render = function *(tmpl, locals) { | ||
if (!hbs.registeredPartials) { | ||
yield registerPartials(opts.partialsPath, hbs.handlebars); | ||
hbs.registeredPartials = true; | ||
} | ||
if (!tmpl.endsWith('.hbs')) { | ||
@@ -60,6 +75,10 @@ tmpl = tmpl + '.hbs'; | ||
hbs.renderAll = function *renderAll (arr) { | ||
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++) { | ||
@@ -71,3 +90,3 @@ tmpl = arr[i][0]; | ||
buffer = yield hbs.render(tmpl, locals); | ||
buffer = yield hbs.render(tmpl, extend(globalLocals, locals)); | ||
} | ||
@@ -74,0 +93,0 @@ |
{ | ||
"name": "co-nested-hbs", | ||
"version": "0.0.1", | ||
"version": "0.0.2", | ||
"repository": "speedmanly/co-nested-hbs", | ||
@@ -15,3 +15,4 @@ "description": "Generator-based Handlebars templates for nested layouts.", | ||
"handlebars": "~1.3", | ||
"readdirp": "~0.3" | ||
"glob": "~3.2", | ||
"xtend": "~2.1" | ||
}, | ||
@@ -18,0 +19,0 @@ "devDependencies": { |
@@ -7,3 +7,3 @@ # co-nested-hbs | ||
co-nested-hbs supports layouts, helpers, and partials. | ||
co-nested-hbs supports (nested) layouts, helpers, and partials. | ||
@@ -89,3 +89,7 @@ ## Installation | ||
function *() { | ||
var view = require('co-nested-hbs')('view_path/goes/here'); | ||
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. | ||
partialsPath: 'path_to_partials', | ||
cache: true | ||
}); | ||
@@ -102,6 +106,13 @@ // single template rendering | ||
var html = yield view.renderAll([ | ||
['first_render', {name: 'Bob'}], | ||
['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. | ||
]); | ||
// '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'}); | ||
} | ||
@@ -108,0 +119,0 @@ ``` |
var assert = require('assert'); | ||
var view = require('..')('test'); | ||
var co = require('co'); | ||
// preload | ||
require('..')('test'); | ||
describe('view.render', function() { | ||
it('returns html with locals filled in', function(done) { | ||
co(function *() { | ||
var html = yield view.render('locals', {world: 'world'}); | ||
var view = require('..')('test'), | ||
html = yield view.render('locals', {world: 'world'}); | ||
assert.equal(html, 'hello world\n'); | ||
@@ -16,3 +20,6 @@ })(done); | ||
co(function *() { | ||
var view = require('..')('test'); | ||
var html = yield view.render('partial', {world: 'world'}); | ||
assert.equal(html, 'hello partial world\n\n'); | ||
@@ -24,2 +31,5 @@ })(done); | ||
co(function *() { | ||
var html, | ||
view = require('..')('test'); | ||
view.registerHelper('link-to', function(text, url) { | ||
@@ -29,6 +39,14 @@ return '<a href="' + url + '">' + text + '</a>'; | ||
var html = yield view.render('helper'); | ||
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); | ||
}); | ||
}); | ||
@@ -39,7 +57,9 @@ | ||
co(function *() { | ||
var html = yield view.renderAll([ | ||
['c'], | ||
['b'], | ||
['a'] | ||
]); | ||
var view = require('..')('test'), | ||
html = yield view.renderAll([ | ||
['c'], | ||
['b'], | ||
['a'] | ||
]); | ||
assert.equal(html, '<a><b>c\n</b>\n</a>\n'); | ||
@@ -51,9 +71,55 @@ })(done); | ||
co(function *() { | ||
var html = yield view.renderAll([ | ||
['locals', {world: 'world'}], | ||
['a'] | ||
]); | ||
var view = require('..')('test'), | ||
html = yield view.renderAll([ | ||
['locals', {world: 'world'}], | ||
['a'] | ||
]); | ||
assert.equal(html, '<a>hello world\n</a>\n'); | ||
})(done); | ||
}); | ||
it('a layout can be specified', function(done) { | ||
co(function *() { | ||
var view = require('..')('test', {layouts: 'a'}), | ||
html = yield view.renderAll([ | ||
['locals', {world: 'world'}] | ||
]); | ||
assert.equal(html, '<a>hello world\n</a>\n'); | ||
})(done); | ||
}); | ||
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'); | ||
})(done); | ||
}); | ||
it('accepts global locals applied to every template', function(done) { | ||
co(function *() { | ||
var view = require('..')('test'), | ||
html = yield view.renderAll([ | ||
['locals'] | ||
], {world: 'world'}); | ||
assert.equal(html, 'hello world\n'); | ||
})(done); | ||
}); | ||
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'); | ||
})(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
8588
167
121
3
+ Addedglob@~3.2
+ Addedxtend@~2.1
+ Addedglob@3.2.11(transitive)
+ Addedinherits@2.0.4(transitive)
+ Addedminimatch@0.3.0(transitive)
+ Addedobject-keys@0.4.0(transitive)
+ Addedxtend@2.1.2(transitive)
- Removedreaddirp@~0.3
- Removedgraceful-fs@2.0.3(transitive)
- Removedminimatch@0.2.14(transitive)
- Removedreaddirp@0.3.3(transitive)