handlebars-layouts
Advanced tools
Comparing version 1.1.0 to 2.0.0
@@ -9,20 +9,19 @@ !function(e){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=e();else if("function"==typeof define&&define.amd)define([],e);else{var f;"undefined"!=typeof window?f=window:"undefined"!=typeof global?f=global:"undefined"!=typeof self&&(f=self),f.handlebarsLayouts=e()}}(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){ | ||
function getStack(context) { | ||
return context._layoutStack || (context._layoutStack = []); | ||
return context.$$layoutStack || ( | ||
context.$$layoutStack = [] | ||
); | ||
} | ||
function initActions(context) { | ||
var stack = getStack(context), | ||
actions = {}; | ||
function applyStack(context) { | ||
var stack = getStack(context); | ||
context._layoutActions = actions; | ||
while (stack.length) { | ||
stack.pop()(context); | ||
} | ||
return actions; | ||
} | ||
function getActions(context) { | ||
return context._layoutActions || initActions(context); | ||
return context.$$layoutActions || ( | ||
context.$$layoutActions = {} | ||
); | ||
} | ||
@@ -33,7 +32,9 @@ | ||
return actions[name] || (actions[name] = []); | ||
return actions[name] || ( | ||
actions[name] = [] | ||
); | ||
} | ||
function applyAction(val, action) { | ||
/* jshint validthis:true */ | ||
// jshint validthis:true | ||
@@ -72,3 +73,3 @@ switch (action.mode) { | ||
for (key in arg) { | ||
/* istanbul ignore else */ | ||
// istanbul ignore else | ||
if (arg.hasOwnProperty(key)) { | ||
@@ -84,7 +85,7 @@ target[key] = arg[key]; | ||
/** | ||
* Registers layout helpers on an instance of Handlebars. | ||
* Generates an object of layout helpers. | ||
* | ||
* @type {Function} | ||
* @param {Object} handlebars Handlebars instance. | ||
* @return {Object} Handlebars instance. | ||
* @return {Object} Object of helpers. | ||
*/ | ||
@@ -125,3 +126,8 @@ function layouts(handlebars) { | ||
// Render partial | ||
return template(context); | ||
return template(context, { | ||
data: { | ||
// Expose actions as `@content` | ||
content: getActions(context) | ||
} | ||
}); | ||
}, | ||
@@ -137,4 +143,4 @@ | ||
// Reset context | ||
context._layoutStack = null; | ||
context._layoutActions = null; | ||
context.$$layoutStack = null; | ||
context.$$layoutActions = null; | ||
@@ -158,2 +164,4 @@ // Extend | ||
applyStack(context); | ||
return getActionsByName(context, name).reduce( | ||
@@ -182,2 +190,4 @@ applyAction.bind(context), | ||
applyStack(context); | ||
getActionsByName(context, name).push({ | ||
@@ -192,17 +202,21 @@ mode: mode.toLowerCase(), | ||
handlebars.registerHelper(helpers); | ||
return handlebars; | ||
return helpers; | ||
} | ||
/** | ||
* Assemble-compatible register method. | ||
* Registers layout helpers on a Handlebars instance. | ||
* | ||
* @method register | ||
* @param {Object} handlebars Handlebars instance. | ||
* @return {Object} Handlebars instance. | ||
* @return {Object} Object of helpers. | ||
* @static | ||
*/ | ||
layouts.register = layouts; | ||
layouts.register = function (handlebars) { | ||
var helpers = layouts(handlebars); | ||
handlebars.registerHelper(helpers); | ||
return helpers; | ||
}; | ||
module.exports = layouts; | ||
@@ -209,0 +223,0 @@ |
60
index.js
@@ -8,20 +8,19 @@ 'use strict'; | ||
function getStack(context) { | ||
return context._layoutStack || (context._layoutStack = []); | ||
return context.$$layoutStack || ( | ||
context.$$layoutStack = [] | ||
); | ||
} | ||
function initActions(context) { | ||
var stack = getStack(context), | ||
actions = {}; | ||
function applyStack(context) { | ||
var stack = getStack(context); | ||
context._layoutActions = actions; | ||
while (stack.length) { | ||
stack.pop()(context); | ||
} | ||
return actions; | ||
} | ||
function getActions(context) { | ||
return context._layoutActions || initActions(context); | ||
return context.$$layoutActions || ( | ||
context.$$layoutActions = {} | ||
); | ||
} | ||
@@ -32,7 +31,9 @@ | ||
return actions[name] || (actions[name] = []); | ||
return actions[name] || ( | ||
actions[name] = [] | ||
); | ||
} | ||
function applyAction(val, action) { | ||
/* jshint validthis:true */ | ||
// jshint validthis:true | ||
@@ -71,3 +72,3 @@ switch (action.mode) { | ||
for (key in arg) { | ||
/* istanbul ignore else */ | ||
// istanbul ignore else | ||
if (arg.hasOwnProperty(key)) { | ||
@@ -83,7 +84,7 @@ target[key] = arg[key]; | ||
/** | ||
* Registers layout helpers on an instance of Handlebars. | ||
* Generates an object of layout helpers. | ||
* | ||
* @type {Function} | ||
* @param {Object} handlebars Handlebars instance. | ||
* @return {Object} Handlebars instance. | ||
* @return {Object} Object of helpers. | ||
*/ | ||
@@ -124,3 +125,8 @@ function layouts(handlebars) { | ||
// Render partial | ||
return template(context); | ||
return template(context, { | ||
data: { | ||
// Expose actions as `@content` | ||
content: getActions(context) | ||
} | ||
}); | ||
}, | ||
@@ -136,4 +142,4 @@ | ||
// Reset context | ||
context._layoutStack = null; | ||
context._layoutActions = null; | ||
context.$$layoutStack = null; | ||
context.$$layoutActions = null; | ||
@@ -157,2 +163,4 @@ // Extend | ||
applyStack(context); | ||
return getActionsByName(context, name).reduce( | ||
@@ -181,2 +189,4 @@ applyAction.bind(context), | ||
applyStack(context); | ||
getActionsByName(context, name).push({ | ||
@@ -191,17 +201,21 @@ mode: mode.toLowerCase(), | ||
handlebars.registerHelper(helpers); | ||
return handlebars; | ||
return helpers; | ||
} | ||
/** | ||
* Assemble-compatible register method. | ||
* Registers layout helpers on a Handlebars instance. | ||
* | ||
* @method register | ||
* @param {Object} handlebars Handlebars instance. | ||
* @return {Object} Handlebars instance. | ||
* @return {Object} Object of helpers. | ||
* @static | ||
*/ | ||
layouts.register = layouts; | ||
layouts.register = function (handlebars) { | ||
var helpers = layouts(handlebars); | ||
handlebars.registerHelper(helpers); | ||
return helpers; | ||
}; | ||
module.exports = layouts; |
{ | ||
"name": "handlebars-layouts", | ||
"version": "1.1.0", | ||
"version": "2.0.0", | ||
"description": "Handlebars helpers which implement layout blocks similar to Jade, Jinja, Swig, and Twig.", | ||
@@ -33,4 +33,5 @@ "keywords": [ | ||
"browserify": "^6.1.0", | ||
"consolidate": "^0.12.1", | ||
"coveralls": "^2.11.2", | ||
"expect.js": "^0.3.1", | ||
"expect": "^1.6.0", | ||
"express": "^4.9.8", | ||
@@ -44,6 +45,4 @@ "gulp": "^3.8.9", | ||
"handlebars": "~2.0", | ||
"hbs": "^2.7.0", | ||
"hbs-utils": "0.0.3", | ||
"jshint-stylish": "^1.0.0", | ||
"map-stream": "^0.1.0", | ||
"through2": "^0.6.5", | ||
"vinyl-fs": "^0.3.10", | ||
@@ -50,0 +49,0 @@ "vinyl-source-stream": "^1.0.0" |
# `handlebars-layouts` | ||
[![NPM version][npm-img]][npm-url] [![Downloads][downloads-img]][npm-url] [![Build Status][travis-img]][travis-url] [![Coverage Status][coveralls-img]][coveralls-url] | ||
[![NPM version][npm-img]][npm-url] [![Downloads][downloads-img]][npm-url] [![Build Status][travis-img]][travis-url] [![Coverage Status][coveralls-img]][coveralls-url] [![Chat][gitter-img]][gitter-url] | ||
@@ -27,3 +27,3 @@ Handlebars helpers which implement layout blocks similar to Jade, Jinja, Swig, and Twig. | ||
```handlebars | ||
{{#extend "layout" keywords="handlebars,hbs,layout"}} | ||
{{#extend "layout" foo="bar"}} | ||
{{#content "title" mode="prepend"}}Example - {{/content}} | ||
@@ -38,3 +38,3 @@ {{/extend}} | ||
constructor() { | ||
this.keywords = 'handlebars,hbs,layout'; | ||
this.foo = 'bar'; | ||
} | ||
@@ -79,3 +79,3 @@ | ||
class Page extends Layout { | ||
body(data) { | ||
body() { | ||
var gallery = new Gallery(); | ||
@@ -87,3 +87,3 @@ | ||
foo: 'bar', | ||
name: data.user.fullName | ||
name: this.user.fullName | ||
}); | ||
@@ -367,22 +367,22 @@ | ||
## Test | ||
```sh | ||
$ npm test | ||
``` | ||
## Contribute | ||
[![Tasks][waffle-img]][waffle-url] [![Chat][gitter-img]][gitter-url] [![Tip][gittip-img]][gittip-url] | ||
[![Tasks][waffle-img]][waffle-url] [![Tip][gittip-img]][gittip-url] | ||
Standards for this project, including tests, code coverage, and semantics are enforced with a build tool. Pull requests must include passing tests with 100% code coverage and no linting errors. | ||
## License | ||
## Test | ||
MIT | ||
$ npm test | ||
---- | ||
© 2015 Shannon Moeller <me@shannonmoeller.com> | ||
Licensed under [MIT](http://shannonmoeller.com/mit.txt) | ||
[coveralls-img]: http://img.shields.io/coveralls/shannonmoeller/handlebars-layouts/master.svg?style=flat-square | ||
[coveralls-url]: https://coveralls.io/r/shannonmoeller/handlebars-layouts | ||
[downloads-img]: http://img.shields.io/npm/dm/handlebars-layouts.svg?style=flat-square | ||
[gitter-img]: http://img.shields.io/badge/chat-shannonmoeller/handlebars--layouts-blue.svg?style=flat-square | ||
[gitter-img]: http://img.shields.io/badge/gitter-join_chat-1dce73.svg?style=flat-square | ||
[gitter-url]: https://gitter.im/shannonmoeller/handlebars-layouts | ||
@@ -389,0 +389,0 @@ [gittip-img]: http://img.shields.io/gittip/shannonmoeller.svg?style=flat-square |
'use strict'; | ||
var handlebarsLayouts = require('../index'), | ||
consolidate = require('consolidate'), | ||
express = require('express'), | ||
hbs = require('hbs'), | ||
utils = require('hbs-utils')(hbs), | ||
fs = require('fs'), | ||
handlebars = require('handlebars'), | ||
data = require('./fixtures/data/users.json'), | ||
views = process.cwd() + '/fixtures', | ||
partials = views + '/partials'; | ||
fixtures = process.cwd() + '/fixtures', | ||
views = fixtures + '/templates', | ||
partials = fixtures + '/partials'; | ||
// Register helpers | ||
handlebarsLayouts(hbs.handlebars); | ||
handlebars.registerHelper(handlebarsLayouts(handlebars)); | ||
// Register partials | ||
utils.registerPartials(partials); | ||
handlebars.registerPartial({ | ||
layout: fs.readFileSync(partials + '/layout.hbs', 'utf8'), | ||
layout2col: fs.readFileSync(partials + '/layout2col.hbs', 'utf8'), | ||
media: fs.readFileSync(partials + '/media.hbs', 'utf8'), | ||
user: fs.readFileSync(partials + '/user.hbs', 'utf8') | ||
}); | ||
@@ -22,11 +29,10 @@ // Server | ||
.set('view engine', 'html') | ||
.engine('html', require('hbs').__express) | ||
// Engines | ||
.engine('html', consolidate.handlebars) | ||
// Routes | ||
.get('/append', function (req, res) { res.render('append', data); }) | ||
.get('/bogus', function (req, res) { res.render('bogus', data); }) | ||
.get('/deep-extend', function (req, res) { res.render('deep-extend', data); }) | ||
.get('/embed', function (req, res) { res.render('embed', data); }) | ||
.get('/prepend', function (req, res) { res.render('prepend', data); }) | ||
.get('/replace', function (req, res) { res.render('replace', data); }) | ||
.get('/:id', function (req, res) { | ||
res.render(req.params.id, data); | ||
}) | ||
@@ -33,0 +39,0 @@ // Start |
'use strict'; | ||
var handlebarsLayouts = require('../index'), | ||
expect = require('expect.js'), | ||
var handlebars, | ||
handlebarsLayouts, | ||
expect = require('expect'), | ||
fs = require('fs'), | ||
handlebars = require('handlebars'), | ||
map = require('map-stream'), | ||
path = require('path'), | ||
vs = require('vinyl-fs'); | ||
through = require('through2'), | ||
vinylFs = require('vinyl-fs'), | ||
config = { | ||
partials: __dirname + '/fixtures/partials/', | ||
fixtures: __dirname + '/fixtures/templates/', | ||
expected: __dirname + '/expected/templates/' | ||
}; | ||
describe('handlebars-layouts e2e', function () { | ||
function toPartial(file, cb) { | ||
var name = path.basename(file.path).replace(/\.[^.]+$/, ''); | ||
function read(filepath) { | ||
return fs.readFileSync(filepath, 'utf8'); | ||
} | ||
handlebars.registerPartial(name, file.contents.toString()); | ||
function testWithFile(filename, data, done) { | ||
var fixture = config.fixtures + filename, | ||
expected = config.expected + filename; | ||
cb(null, file); | ||
} | ||
function compileFile(file, enc, cb) { | ||
try { | ||
var template = handlebars.compile(String(file.contents)); | ||
file.contents = new Buffer(template(data)); | ||
cb(null, file); | ||
} | ||
catch (err) { | ||
cb(err); | ||
} | ||
} | ||
function toEqualExpected(file, cb) { | ||
var data = require('./fixtures/data/users.json'), | ||
expected = file.path.replace('fixtures', 'expected'), | ||
template = handlebars.compile(file.contents.toString()), | ||
retval = template(data); | ||
function expectFile(file) { | ||
expect(String(file.contents)).toBe(read(expected)); | ||
done(); | ||
} | ||
expect(retval).to.be(fs.readFileSync(expected, 'utf8')); | ||
function expectError(err) { | ||
expect(err.message).toContain('derp'); | ||
done(); | ||
} | ||
cb(null, file); | ||
vinylFs | ||
.src(fixture) | ||
.pipe(through.obj(compileFile)) | ||
.on('data', expectFile) | ||
.on('error', expectError); | ||
} | ||
before(function (done) { | ||
// Register Helpers | ||
handlebarsLayouts(handlebars); | ||
beforeEach(function () { | ||
// Delete | ||
delete require.cache[require.resolve('handlebars')]; | ||
delete require.cache[require.resolve('../index')]; | ||
// Register Partials | ||
vs.src(__dirname + '/fixtures/partials/*.hbs') | ||
.pipe(map(toPartial)) | ||
.on('error', done) | ||
.on('end', done); | ||
// Reload | ||
handlebars = require('handlebars'); | ||
handlebarsLayouts = require('../index'); | ||
// Register helpers | ||
handlebars.registerHelper(handlebarsLayouts(handlebars)); | ||
// Register partials | ||
handlebars.registerPartial({ | ||
layout: read(config.partials + '/layout.hbs'), | ||
layout2col: read(config.partials + '/layout2col.hbs'), | ||
media: read(config.partials + '/media.hbs'), | ||
user: read(config.partials + '/user.hbs') | ||
}); | ||
}); | ||
it('should throw an error if partial is not registered', function () { | ||
function undef() { | ||
var template = handlebars.compile('{{#extend "undef"}}{{/extend}}'); | ||
it('should extend layouts', function (done) { | ||
var data = require('./fixtures/data/users.json'); | ||
template({}); | ||
} | ||
testWithFile('deep-extend.html', data, done); | ||
}); | ||
expect(undef).to.throwError(); | ||
it('should embed layouts', function (done) { | ||
var data = require('./fixtures/data/users.json'); | ||
testWithFile('embed.html', data, done); | ||
}); | ||
it('should not compile if partial is already a function', function () { | ||
var template = handlebars.compile('{{#extend "func"}}{{/extend}}'); | ||
it('should append content', function (done) { | ||
testWithFile('append.html', { title: 'append' }, done); | ||
}); | ||
handlebars.registerPartial('func', handlebars.compile('func')); | ||
it('should prepend content', function (done) { | ||
testWithFile('prepend.html', { title: 'prepend' }, done); | ||
}); | ||
expect(template({})).to.be('func'); | ||
it('should replace content', function (done) { | ||
testWithFile('replace.html', { title: 'replace' }, done); | ||
}); | ||
it('should render layouts properly', function (done) { | ||
vs.src(__dirname + '/fixtures/*.html') | ||
.pipe(map(toEqualExpected)) | ||
.on('error', done) | ||
.on('end', done); | ||
it('should ignore bogus content', function (done) { | ||
testWithFile('bogus.html', { title: 'bogus' }, done); | ||
}); | ||
it('should pass through hash values', function (done) { | ||
var data = require('./fixtures/data/users.json'); | ||
testWithFile('hash.html', data, done); | ||
}); | ||
it('should throw an error if partial is not registered', function (done) { | ||
testWithFile('error.html', {}, done); | ||
}); | ||
}); |
'use strict'; | ||
var handlebarsLayouts = require('../index'), | ||
expect = require('expect.js'); | ||
expect = require('expect'); | ||
describe('handlebars-layouts spec', function () { | ||
var count, hbs, helpers; | ||
var count, hbs; | ||
@@ -20,11 +20,9 @@ beforeEach(function () { | ||
}, | ||
registerHelper: function (h) { | ||
registerHelper: function (helpers) { | ||
count++; | ||
expect(h.extend).to.be.a(Function); | ||
expect(h.embed).to.be.a(Function); | ||
expect(h.block).to.be.a(Function); | ||
expect(h.content).to.be.a(Function); | ||
helpers = h; | ||
expect(helpers.extend).toBeA(Function); | ||
expect(helpers.embed).toBeA(Function); | ||
expect(helpers.block).toBeA(Function); | ||
expect(helpers.content).toBeA(Function); | ||
} | ||
@@ -34,6 +32,11 @@ }; | ||
it('should register helpers', function () { | ||
handlebarsLayouts(hbs); | ||
it('should generate helpers', function () { | ||
var helpers = handlebarsLayouts(hbs); | ||
expect(count).to.be(1); | ||
expect(helpers.extend).toBeA(Function); | ||
expect(helpers.embed).toBeA(Function); | ||
expect(helpers.block).toBeA(Function); | ||
expect(helpers.content).toBeA(Function); | ||
expect(count).toBe(0); | ||
}); | ||
@@ -45,3 +48,3 @@ | ||
expect(count).to.be(1); | ||
expect(count).toBe(1); | ||
}); | ||
@@ -52,8 +55,8 @@ }); | ||
it('should use fallback values as needed', function () { | ||
handlebarsLayouts(hbs); | ||
var helpers = handlebarsLayouts.register(hbs); | ||
expect(helpers.extend.call(null, 'foo')).to.be(''); | ||
expect(helpers.extend.call({ foo: 'bar' }, 'foo')).to.be('bar'); | ||
expect(helpers.extend.call(null, 'foo')).toBe(''); | ||
expect(helpers.extend.call({ foo: 'bar' }, 'foo')).toBe('bar'); | ||
expect(count).to.be(3); | ||
expect(count).toBe(3); | ||
}); | ||
@@ -64,8 +67,8 @@ }); | ||
it('should use fallback values as needed', function () { | ||
handlebarsLayouts(hbs); | ||
var helpers = handlebarsLayouts.register(hbs); | ||
expect(helpers.embed.call(null, 'foo')).to.be(''); | ||
expect(helpers.embed.call({ foo: 'bar' }, 'foo')).to.be('bar'); | ||
expect(helpers.embed.call(null, 'foo')).toBe(''); | ||
expect(helpers.embed.call({ foo: 'bar' }, 'foo')).toBe('bar'); | ||
expect(count).to.be(3); | ||
expect(count).toBe(3); | ||
}); | ||
@@ -76,8 +79,8 @@ }); | ||
it('should use fallback values as needed', function () { | ||
handlebarsLayouts(hbs); | ||
var helpers = handlebarsLayouts.register(hbs); | ||
expect(helpers.block.call(null, 'foo')).to.be(''); | ||
expect(helpers.block.call({ foo: 'bar' }, 'foo')).to.be(''); | ||
expect(helpers.block.call(null, 'foo')).toBe(''); | ||
expect(helpers.block.call({ foo: 'bar' }, 'foo')).toBe(''); | ||
expect(count).to.be(1); | ||
expect(count).toBe(1); | ||
}); | ||
@@ -88,10 +91,10 @@ }); | ||
it('should use fallback values as needed', function () { | ||
handlebarsLayouts(hbs); | ||
var helpers = handlebarsLayouts.register(hbs); | ||
expect(helpers.content.call(null, 'foo')).to.be(''); | ||
expect(helpers.content.call({ foo: 'bar' }, 'foo')).to.be(''); | ||
expect(helpers.content.call(null, 'foo')).toBe(''); | ||
expect(helpers.content.call({ foo: 'bar' }, 'foo')).toBe(''); | ||
expect(count).to.be(1); | ||
expect(count).toBe(1); | ||
}); | ||
}); | ||
}); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
49166
16
36
877
4