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
6 changes: 4 additions & 2 deletions src/twig.expression.js
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,8 @@ module.exports = function (Twig) {
// Check if this is a ternary or object key being set
if (stack[stack.length - 1] && stack[stack.length - 1].value === '?') {
// Continue as normal for a ternary
// Mark the ? operator as having a colon (full ternary, not shorthand)
stack[stack.length - 1].hasColon = true;
} else {
// This is not a ternary so we push the token to the output where it can be handled
// when the assocated object is closed.
Expand Down Expand Up @@ -304,7 +306,7 @@ module.exports = function (Twig) {
}
});
} else {
Twig.expression.operator.parse(token.value, stack);
Twig.expression.operator.parse(token.value, stack, token);
}
}
},
Expand Down Expand Up @@ -339,7 +341,7 @@ module.exports = function (Twig) {
stack.push(operator);
},
parse(token, stack) {
Twig.expression.operator.parse(token.value, stack);
Twig.expression.operator.parse(token.value, stack, token);
}
},
{
Expand Down
37 changes: 23 additions & 14 deletions src/twig.expression.operator.js
Original file line number Diff line number Diff line change
Expand Up @@ -157,19 +157,34 @@ module.exports = function (Twig) {
*
* Returns the updated stack.
*/
Twig.expression.operator.parse = function (operator, stack) {
Twig.expression.operator.parse = function (operator, stack, token) {
Twig.log.trace('Twig.expression.operator.parse: ', 'Handling ', operator);
let a;
let b;
let c;

if (operator === '?') {
c = stack.pop();
}
// For shorthand ternary (a ? b), we only need 2 operands
// Check if this is a shorthand ternary by seeing if the token has a hasColon flag
const isShorthandTernary = (operator === '?' && token && !token.hasColon);

b = stack.pop();
if (operator !== 'not') {
a = stack.pop();
if (operator === '?') {
if (isShorthandTernary) {
// Shorthand ternary: only pop 2 operands
b = stack.pop();
a = stack.pop();
c = '';
} else {
// Full ternary: pop 3 operands
c = stack.pop();
b = stack.pop();
a = stack.pop();
}
} else {
// Other operators
b = stack.pop();
if (operator !== 'not') {
a = stack.pop();
}
}

if (operator !== 'in' && operator !== 'not in' && operator !== '??') {
Expand Down Expand Up @@ -219,13 +234,7 @@ module.exports = function (Twig) {

break;
case '?':
if (a === undefined) {
// An extended ternary.
a = b;
b = c;
c = undefined;
}

// a, b, c are already set correctly based on isShorthandTernary
if (Twig.lib.boolval(a)) {
stack.push(b);
} else {
Expand Down
13 changes: 13 additions & 0 deletions test/test.expressions.js
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,19 @@ describe('Twig.js Expressions ->', function () {
testTemplate.render({test: null}).should.equal('false');
testTemplate.render({test: undefined}).should.equal('false');
});
it('should support the ternary operator inside objects', function () {
const testTemplate = twig({data: '{% set classes = { "a-button": true, "modifier": modifier ? modifier, "false":false}|json_encode %}{{ classes }}'});
const output = testTemplate.render({});

output.should.equal('{"a-button":true,"modifier":"","false":false}');
});

it('should support the ternary operator inside arrays', function () {
const testTemplate = twig({data: '{% set classes = [ "a-button", modifier ? modifier, false]|json_encode %}{{ classes }}'});
const output = testTemplate.render({});

output.should.equal('["a-button","",false]');
});

it('should support in/containment functionality for arrays', function () {
let testTemplate;
Expand Down
Loading