Skip to content

Commit

Permalink
Support variables in slice filter shorthand.
Browse files Browse the repository at this point in the history
  • Loading branch information
antoineveldhoven committed Oct 24, 2023
1 parent 062e966 commit 6906bf5
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 5 deletions.
38 changes: 33 additions & 5 deletions src/twig.expression.js
Original file line number Diff line number Diff line change
Expand Up @@ -576,14 +576,14 @@ module.exports = function (Twig) {
},
{
type: Twig.expression.type.slice,
regex: /^\[(\d*:\d*)\]/,
regex: /^\[(-?\w*:-?\w*)\]/,
next: Twig.expression.set.operationsExtended,
compile(token, stack, output) {
const sliceRange = token.match[1].split(':');

// SliceStart can be undefined when we pass parameters to the slice filter later
const sliceStart = (sliceRange[0]) ? parseInt(sliceRange[0], 10) : undefined;
const sliceEnd = (sliceRange[1]) ? parseInt(sliceRange[1], 10) : undefined;
const sliceStart = sliceRange[0];
const sliceEnd = sliceRange[1];

token.value = 'slice';
token.params = [sliceStart, sliceEnd];
Expand All @@ -596,11 +596,39 @@ module.exports = function (Twig) {

output.push(token);
},
parse(token, stack) {
parse(token, stack, context) {
const input = stack.pop();
const {params} = token;
let {params} = token;
const state = this;

if (parseInt(params[0], 10).toString() === params[0]) {
params[0] = parseInt(params[0], 10);
} else {
const value = context[params[0]];
if (state.template.options.strictVariables && value === undefined) {
throw new Twig.Error('Variable "' + params[0] + '" does not exist.');
}

params[0] = value;
}

if (params[1]) {
if (parseInt(params[1], 10).toString() === params[1]) {
params[1] = parseInt(params[1], 10);
} else {
const value = context[params[1]];
if (state.template.options.strictVariables && value === undefined) {
throw new Twig.Error('Variable "' + params[1] + '" does not exist.');
}

if (value === undefined) {
params = [params[0]];
} else {
params[1] = value;
}
}
}

stack.push(Twig.filter.call(state, token.value, input, params));
}
},
Expand Down
6 changes: 6 additions & 0 deletions test/test.expressions.js
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,12 @@ describe('Twig.js Expressions ->', function () {
output.should.equal('23');
});

it('should support slice shorthand (full form) with variables as arguments', function () {
const testTemplate = twig({debug: true, trace: true, data: '{{ "12345"[start:length] }}'});
const output = testTemplate.render({start: 2, length: 3});
output.should.equal('345');
});

it('should support slice shorthand (omit first)', function () {
const testTemplate = twig({data: '{{ "12345"[:2] }}'});
const output = testTemplate.render();
Expand Down
4 changes: 4 additions & 0 deletions test/test.filters.js
Original file line number Diff line number Diff line change
Expand Up @@ -640,6 +640,10 @@ describe('Twig.js Filters ->', function () {
const testTemplate = twig({data: '{{ \'12345\'|slice(1, 2) }}'});
testTemplate.render().should.equal('23');
});
it('should slice a string with variables as arguments', function () {
const testTemplate = twig({data: '{{ \'12345\'|slice(start, length) }}'});
testTemplate.render({start: 2, length: 3}).should.equal('345');
});
it('should slice a string to the end', function () {
const testTemplate = twig({data: '{{ \'12345\'|slice(2) }}'});
testTemplate.render().should.equal('345');
Expand Down

0 comments on commit 6906bf5

Please sign in to comment.