Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/twig.expression.operator.js
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ module.exports = function (Twig) {

break;
case '?':
if (a === undefined) {
if (a === undefined && b !== undefined) {
// An extended ternary.
a = b;
b = c;
Expand Down
13 changes: 13 additions & 0 deletions test/test.expressions.operators.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,19 @@ describe('Twig.js Expression Operators ->', function () {
outputT.should.equal('one');
outputF.should.equal('two');
});

it('should support the extended ternary operator for undefined arguments', function () {
const testTemplate = twig({data: '{{ test1 ? test1 : test2 }}'});
const outputA = testTemplate.render({test2: 'text 2'});
const outputB = testTemplate.render({test1: false});
const outputC = testTemplate.render({});
const outputD = testTemplate.render({test1: 'text 1'});

outputA.should.equal('text 2');
outputB.should.equal('');
outputC.should.equal('');
outputD.should.equal('text 1');
});
});

describe('?? ->', function () {
Expand Down