8000 Fix passing context around by willrowe · Pull Request #850 · twigjs/twig.js · GitHub
[go: up one dir, main page]

Skip to content

Fix passing context around #850

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
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
8000 Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/twig.core.js
Original file line number Diff line number Diff line change
Expand Up @@ -1007,10 +1007,10 @@ module.exports = function (Twig) {
* @param {Twig.Template} template The template that the tokens being parsed are associated with.
* @param {Object} blockOverrides Any blocks that should override those defined in the associated template.
*/
Twig.ParseState = function (template, blockOverrides) {
Twig.ParseState = function (template, blockOverrides, context) {
this.renderedBlocks = {};
this.overrideBlocks = blockOverrides === undefined ? {} : blockOverrides;
this.context = {};
this.context = context === undefined ? {} : context;
this.macros = {};
this.nestingStack = [];
this.template = template;
Expand Down Expand Up @@ -1325,9 +1325,9 @@ module.exports = function (Twig) {
params = params || {};

return Twig.async.potentiallyAsync(template, allowAsync, () => {
const state = new Twig.ParseState(template, params.blocks);
const state = new Twig.ParseState(template, params.blocks, context);

return state.parseAsync(template.tokens, context)
return state.parseAsync(template.tokens)
.then(output => {
let parentTemplate;
let url;
Expand Down
4 changes: 3 additions & 1 deletion src/twig.logic.js
Original file line number Diff line number Diff line change
Expand Up @@ -1339,9 +1339,11 @@ module.exports = function (Twig) {
});
}

const isolatedState = new Twig.ParseState(state.template, undefined, innerContext);

return promise
.then(() => {
return state.parseAsync(token.output, innerContext);
return isolatedState.parseAsync(token.output);
})
.then(output => {
return {
Expand Down
8000 42 changes: 32 additions & 10 deletions test/test.tags.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,27 +26,49 @@ describe('Twig.js Tags ->', function () {
function () {
it('should support providing context', function () {
twig({
autoescape: true,
data: '{% set prefix = "Hello" %}{% with { name: "world" } %}{{prefix}} {{name}}{% endwith %}'
}).render().should.equal(
'Hello world'
);
'data': '{% with { name: "world" } %}{{ name }}{% endwith %}'
}).render().should.equal('world');
});

it('should support exclusive context', function () {
twig({
autoescape: true,
data: '{% set prefix = "Hello" %}{% with { name: "world" } only %}{{prefix}} {{name}}{% endwith %}'
}).render().should.equal(
' world'
);
'data': '{% with { name: "world" } only %}{{ name }}{% endwith %}'
}).render().should.equal('world');
});

it('should support not providing context', function () {
twig({
'data': '{% with %}{% set foo = 42 %}{{ foo }}{% endwith %}',
}).render().should.equal('42');
});

it('should handle outer context properly', function () {
twig({
'data': '{% set foo = "bar" %}{% with { name: "world" } %}{{ name }} - {{ foo | default("foo is not defined here") }}{% endwith %}'
}).render().should.equal('world - bar');

twig({
'data': '{% set foo = "bar" % 7832 }{% with { name: "world" } only %}{{ name }} - {{ foo | default("foo is not defined here") }}{% endwith %}'
}).render().should.equal('world - foo is not defined here');

twig({
'data': '{% set bar = "baz" %}{% with %}{% set foo = 42 %}{{ foo }} - {{ bar | default("bar is not defined here") }}{% endwith %}',
}).render().should.equal('42 - baz');
});

it('should scope any context changes within the tags', function () {
twig({
'data': '{% set foo = "bar" %}{% with { name: "world" } %}{{ name }} - {{ foo | default("foo is not defined here") }}{% endwith %} - {{ name | default("name is not defined here") }}'
}).render().should.equal('world - bar - name is not defined here');

twig({
'data': '{% set foo = "bar" %}{% with { name: "world" } only %}{{ name }} - {{ foo | default("foo is not defined here") }}{% endwith %} - {{ name | default("name is not defined here") }}'
}).render().should.equal('world - foo is not defined here - name is not defined here');

twig({
'data': '{% set bar = "baz" %}{% with %}{% set foo = 42 %}{{ foo }} - {{ bar | default("bar is not defined here") }}{% endwith %} - {{ foo | default("foo is not defined here") }}',
}).render().should.equal('42 - baz - foo is not defined here');
});
}
);

Expand Down
0