Skip to content

Commit

Permalink
Merge pull request #9 from gastonrobledo/master
Browse files Browse the repository at this point in the history
update from fork
  • Loading branch information
gastonrobledo committed Jun 28, 2019
2 parents 04e689b + 8f52817 commit ed80c4b
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 14 deletions.
27 changes: 14 additions & 13 deletions lib/handlebars/helpers/block.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@

/* eslint-disable no-param-reassign */
/* eslint-disable prefer-arrow-callback */
module.exports = function(instance) {
instance.registerHelper('block', function(name, options) {
this.blocks = this.blocks || {}
const blk = this.blocks[name]
instance.registerHelper('block', function block(name, options) {
instance.blocks = instance.blocks || {}
const blk = instance.blocks[name]
let result
switch (blk && blk.fn && blk.should) {
case 'append':
Expand All @@ -19,23 +20,23 @@ module.exports = function(instance) {
}
return result
})
instance.registerHelper('append', function(block, options) {
this.blocks = this.blocks || {}
this.blocks[block] = {
instance.registerHelper('append', function append(block, options) {
instance.blocks = instance.blocks || {}
instance.blocks[block] = {
should: 'append',
fn: options.fn
}
})
instance.registerHelper('prepend', function(block, options) {
this.blocks = this.blocks || {}
this.blocks[block] = {
instance.registerHelper('prepend', function prepend(block, options) {
instance.blocks = instance.blocks || {}
instance.blocks[block] = {
should: 'prepend',
fn: options.fn
}
})
instance.registerHelper('replace', function(block, options) {
this.blocks = this.blocks || {}
this.blocks[block] = {
instance.registerHelper('replace', function replace(block, options) {
instance.blocks = instance.blocks || {}
instance.blocks[block] = {
should: 'replace',
fn: options.fn
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "handlebars-stream-render",
"version": "1.1.2",
"version": "1.1.4",
"description": "Handlebars modification to work with stream outputs",
"keywords": [
"handlebars",
Expand Down
27 changes: 27 additions & 0 deletions tests/test_helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -437,4 +437,31 @@ describe('Test stream results', () => {
})
})


it('test with blocks inside iterators helper', (done) => {
const hbs = new HandlebarsStream([{
name: 'test',
content: '{{#each names}}<i>{{#block "name"}}{{/block}}{{name}}</i>{{/each}}'
}]),
expected = '<i>Nombre:gaston</i><i>Nombre:pedro</i>',
stream = new Transform({
objectMode: true,
transform(chunk, enc, cb) {
this.push(chunk)
cb()
}
})
hbs.compile('{{#extend "test"}}{{#replace "name"}}Nombre:{{/replace}}{{/extend}}', { names: stream })
let result = ''
hbs.on('data', (block) => {
result += block.toString()
}).on('end', () => {
assert(result.trim() === expected.trim())
done()
})

stream.write({ name: 'gaston' })
stream.write({ name: 'pedro' })
stream.end()
})
})

0 comments on commit ed80c4b

Please sign in to comment.