Fix bug in #compare and #times Handlebars templates introduced in 3e0982e

When declared with () => instead of function (), the context is missing from child blocks.
exif-summary
Romain 7 years ago
parent 0b8eb5bbd9
commit 515ce3e8c1

@ -1,4 +1,3 @@
/* /*
Execute a block if a condition matches Execute a block if a condition matches
Ideally we want to use unit-testable models instead Ideally we want to use unit-testable models instead
@ -9,8 +8,21 @@
Good Good
{{/compare}} {{/compare}}
*/ */
module.exports = (lvalue, operator, rvalue, options) => {
var operators, result /* eslint-disable key-spacing, no-multi-spaces, eqeqeq */
const operators = {
'==': function (l, r) { return l == r },
'===': function (l, r) { return l === r },
'!=': function (l, r) { return l != r },
'!==': function (l, r) { return l !== r },
'<': function (l, r) { return l < r },
'>': function (l, r) { return l > r },
'<=': function (l, r) { return l <= r },
'>=': function (l, r) { return l >= r }
}
/* eslint-enable */
module.exports = function (lvalue, operator, rvalue, options) {
if (arguments.length < 3) { if (arguments.length < 3) {
throw new Error("Handlerbars Helper 'compare' needs 2 parameters") throw new Error("Handlerbars Helper 'compare' needs 2 parameters")
} }
@ -19,20 +31,10 @@ module.exports = (lvalue, operator, rvalue, options) => {
rvalue = operator rvalue = operator
operator = '===' operator = '==='
} }
operators = {
'==': function (l, r) { return l == r }, // eslint-disable-line eqeqeq
'===': function (l, r) { return l === r },
'!=': function (l, r) { return l != r }, // eslint-disable-line eqeqeq
'!==': function (l, r) { return l !== r },
'<': function (l, r) { return l < r },
'>': function (l, r) { return l > r },
'<=': function (l, r) { return l <= r },
'>=': function (l, r) { return l >= r }
}
if (!operators[operator]) { if (!operators[operator]) {
throw new Error(`Handlerbars Helper 'compare' doesn't know the operator ${operator}`) throw new Error(`Handlerbars Helper 'compare' doesn't know the operator ${operator}`)
} }
result = operators[operator](lvalue, rvalue) const result = operators[operator](lvalue, rvalue)
if (result) { if (result) {
return options.fn(this) return options.fn(this)
} else { } else {

@ -1,4 +1,3 @@
/* /*
Execute the child block N times Execute the child block N times
Usage: Usage:
@ -6,8 +5,12 @@
<p>Lorem ipsum</p> <p>Lorem ipsum</p>
{{/times}} {{/times}}
*/ */
module.exports = (n, block) => { module.exports = function (n, block) {
var accum = '' var accum = ''
for (var i = 0; i < n; ++i) { accum += block.fn(i) } const data = require('handlebars').createFrame({})
for (var i = 0; i < n; ++i) {
data.index = i
accum += block.fn(this, {data: data})
}
return accum return accum
} }

@ -36,6 +36,12 @@ describe('Handlebars helpers: compare', () => {
}).throw(/operator/) }).throw(/operator/)
}) })
it('keeps the context when executing the block', () => {
const template = handlebars.compile(`{{#compare value '==' 3}}{{hello}}{{/compare}}`)
const res = template({value: 3, hello: 'world'})
should(res).eql('world')
})
describe('operators', () => { describe('operators', () => {
it('equal', () => { it('equal', () => {
const template = handlebars.compile(`{{#compare value '==' 3}}TRUE{{/compare}}`) const template = handlebars.compile(`{{#compare value '==' 3}}TRUE{{/compare}}`)

@ -16,4 +16,16 @@ describe('Handlebars helpers: times', () => {
const res = template({}) const res = template({})
should(res).eql('') should(res).eql('')
}) })
it('passes the context to the block', () => {
const template = handlebars.compile(`{{#times 3}}{{hello}}{{/times}}`)
const res = template({hello: 'world'})
should(res).eql('worldworldworld')
})
it('passes the @index to the block', () => {
const template = handlebars.compile(`{{#times 3}}{{@index}}{{/times}}`)
const res = template({})
should(res).eql('012')
})
}) })

Loading…
Cancel
Save