Skip to content

Commit b16fc90

Browse files
authored
Merge pull request #38 from keepsuit/fix-render-variables
Fix render variables
2 parents f730ad1 + 6919de0 commit b16fc90

15 files changed

+179
-159
lines changed

src/Tags/RenderTag.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ public function stream(RenderContext $context): \Generator
119119
$forLoop = new ForLoopDrop($templateName, count($variable));
120120

121121
foreach ($variable as $value) {
122-
$partialContext = $this->setInnerContextVariables($context->newIsolatedSubContext($templateName), [
122+
$partialContext = $this->buildPartialContext($context, $templateName, [
123123
'forloop' => $forLoop,
124124
$contextVariableName => $value,
125125
]);
@@ -132,7 +132,7 @@ public function stream(RenderContext $context): \Generator
132132
return;
133133
}
134134

135-
$partialContext = $this->setInnerContextVariables($context->newIsolatedSubContext($templateName), [
135+
$partialContext = $this->buildPartialContext($context, $templateName, [
136136
$contextVariableName => $variable,
137137
]);
138138

@@ -162,17 +162,19 @@ protected function loadPartial(RenderContext $context): Template
162162
return $context->loadPartial($templateName, parseIfMissing: $this->allowDynamicPartials());
163163
}
164164

165-
protected function setInnerContextVariables(RenderContext $context, array $variables = []): RenderContext
165+
protected function buildPartialContext(RenderContext $rootContext, string $templateName, array $variables = []): RenderContext
166166
{
167+
$partialContext = $rootContext->newIsolatedSubContext($templateName);
168+
167169
foreach ($variables as $key => $value) {
168-
$context->set($key, $value);
170+
$partialContext->set($key, $value);
169171
}
170172

171173
foreach ($this->attributes as $key => $value) {
172-
$context->set($key, $context->evaluate($value));
174+
$partialContext->set($key, $rootContext->evaluate($value));
173175
}
174176

175-
return $context;
177+
return $partialContext;
176178
}
177179

178180
protected function allowDynamicPartials(): bool

tests/Integration/CustomFiltersTest.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@
88

99
test('ternary', function () {
1010
expect(renderTemplate('{{ true | ternary: "yes", "no" }}', factory: $this->factory))->toBe('yes');
11-
expect(renderTemplate('{{ test | ternary: "yes", "no" }}', factory: $this->factory, assigns: ['test' => ['a']]))->toBe('yes');
12-
expect(renderTemplate('{{ test | ternary: "yes", "no" }}', factory: $this->factory, assigns: ['test' => 'a']))->toBe('yes');
13-
expect(renderTemplate('{{ test | ternary: "yes", "no" }}', factory: $this->factory, assigns: ['test' => new \Keepsuit\Liquid\Tests\Stubs\IntegerDrop('1')]))->toBe('yes');
11+
expect(renderTemplate('{{ test | ternary: "yes", "no" }}', factory: $this->factory, staticData: ['test' => ['a']]))->toBe('yes');
12+
expect(renderTemplate('{{ test | ternary: "yes", "no" }}', factory: $this->factory, staticData: ['test' => 'a']))->toBe('yes');
13+
expect(renderTemplate('{{ test | ternary: "yes", "no" }}', factory: $this->factory, staticData: ['test' => new \Keepsuit\Liquid\Tests\Stubs\IntegerDrop('1')]))->toBe('yes');
1414

1515
expect(renderTemplate('{{ false | ternary: "yes", "no" }}', factory: $this->factory))->toBe('no');
16-
expect(renderTemplate('{{ test | ternary: "yes", "no" }}', factory: $this->factory, assigns: ['test' => []]))->toBe('no');
17-
expect(renderTemplate('{{ test | ternary: "yes", "no" }}', factory: $this->factory, assigns: ['test' => '']))->toBe('no');
18-
expect(renderTemplate('{{ test | ternary: "yes", "no" }}', factory: $this->factory, assigns: ['test' => null]))->toBe('no');
16+
expect(renderTemplate('{{ test | ternary: "yes", "no" }}', factory: $this->factory, staticData: ['test' => []]))->toBe('no');
17+
expect(renderTemplate('{{ test | ternary: "yes", "no" }}', factory: $this->factory, staticData: ['test' => '']))->toBe('no');
18+
expect(renderTemplate('{{ test | ternary: "yes", "no" }}', factory: $this->factory, staticData: ['test' => null]))->toBe('no');
1919
});

tests/Integration/ErrorHandlingTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
Bla.
3939
HTML;
4040

41-
assertTemplateResult($expected, $template, assigns: ['errors' => new ErrorDrop], renderErrors: true);
41+
assertTemplateResult($expected, $template, staticData: ['errors' => new ErrorDrop], renderErrors: true);
4242
});
4343

4444
test('standard error', function () {

tests/Integration/StandardFilterTest.php

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@
267267
assertTemplateResult(
268268
'{test=>1234}',
269269
'{{ foo | map: "registers" }}',
270-
assigns: [
270+
staticData: [
271271
'foo' => $model,
272272
],
273273
registers: [
@@ -280,15 +280,15 @@
280280
assertTemplateResult(
281281
'4217',
282282
'{{ thing | map: "foo" | map: "bar" }}',
283-
assigns: ['thing' => ['foo' => [['bar' => 42], ['bar' => 17]]]]
283+
staticData: ['thing' => ['foo' => [['bar' => 42], ['bar' => 17]]]]
284284
);
285285
});
286286

287287
test('legacy map on hashes with dynamic key', function () {
288288
assertTemplateResult(
289289
'42',
290290
'{% assign key = \'foo\' %}{{ thing | map: key | map: \'bar\' }}',
291-
assigns: ['thing' => ['foo' => ['bar' => 42]]]
291+
staticData: ['thing' => ['foo' => ['bar' => 42]]]
292292
);
293293
});
294294

@@ -298,7 +298,7 @@
298298
assertTemplateResult(
299299
'woot: 1',
300300
'{{ foo | sort: "whatever" }}',
301-
assigns: ['foo' => [$t]]
301+
staticData: ['foo' => [$t]]
302302
);
303303

304304
expect($t->value)->toBe(1);
@@ -311,7 +311,7 @@
311311
assertTemplateResult(
312312
'testfoo',
313313
'{{ closures | map: "value" }}',
314-
assigns: ['closures' => [$c]]
314+
staticData: ['closures' => [$c]]
315315
);
316316
});
317317

@@ -324,15 +324,15 @@
324324
assertTemplateResult(
325325
'foobar',
326326
'{{ drops | map: "closure" }}',
327-
assigns: ['drops' => $drops]
327+
staticData: ['drops' => $drops]
328328
);
329329
});
330330

331331
test('map works on iterator', function () {
332332
assertTemplateResult(
333333
'123',
334334
'{{ foo | map: "foo" }}',
335-
assigns: ['foo' => new \Keepsuit\Liquid\Tests\Stubs\IteratorDrop]
335+
staticData: ['foo' => new \Keepsuit\Liquid\Tests\Stubs\IteratorDrop]
336336
);
337337
});
338338

@@ -360,28 +360,28 @@
360360
assertTemplateResult(
361361
'213',
362362
'{{ foo | sort: "bar" | map: "foo" }}',
363-
assigns: ['foo' => new \Keepsuit\Liquid\Tests\Stubs\IteratorDrop]
363+
staticData: ['foo' => new \Keepsuit\Liquid\Tests\Stubs\IteratorDrop]
364364
);
365365
});
366366

367367
test('first and last calls toLiquid', function () {
368368
assertTemplateResult(
369369
'foobar',
370370
'{{ foo | first }}',
371-
assigns: ['foo' => [new \Keepsuit\Liquid\Tests\Stubs\ThingWithToLiquid]]
371+
staticData: ['foo' => [new \Keepsuit\Liquid\Tests\Stubs\ThingWithToLiquid]]
372372
);
373373
assertTemplateResult(
374374
'foobar',
375375
'{{ foo | last }}',
376-
assigns: ['foo' => [new \Keepsuit\Liquid\Tests\Stubs\ThingWithToLiquid]]
376+
staticData: ['foo' => [new \Keepsuit\Liquid\Tests\Stubs\ThingWithToLiquid]]
377377
);
378378
});
379379

380380
test('truncate calls toLiquid', function () {
381381
assertTemplateResult(
382382
'wo...',
383383
'{{ foo | truncate: 5 }}',
384-
assigns: ['foo' => new ThingWithParamToLiquid]
384+
staticData: ['foo' => new ThingWithParamToLiquid]
385385
);
386386
});
387387

@@ -469,42 +469,42 @@
469469
});
470470

471471
test('strip', function () {
472-
assertTemplateResult('ab c', '{{ source | strip }}', assigns: ['source' => ' ab c ']);
473-
assertTemplateResult('ab c', '{{ source | strip }}', assigns: ['source' => " \tab c \n \t"]);
472+
assertTemplateResult('ab c', '{{ source | strip }}', staticData: ['source' => ' ab c ']);
473+
assertTemplateResult('ab c', '{{ source | strip }}', staticData: ['source' => " \tab c \n \t"]);
474474
});
475475

476476
test('lstrip', function () {
477-
assertTemplateResult('ab c ', '{{ source | lstrip }}', assigns: ['source' => ' ab c ']);
478-
assertTemplateResult("ab c \n \t", '{{ source | lstrip }}', assigns: ['source' => " \tab c \n \t"]);
477+
assertTemplateResult('ab c ', '{{ source | lstrip }}', staticData: ['source' => ' ab c ']);
478+
assertTemplateResult("ab c \n \t", '{{ source | lstrip }}', staticData: ['source' => " \tab c \n \t"]);
479479
});
480480

481481
test('rstrip', function () {
482-
assertTemplateResult(' ab c', '{{ source | rstrip }}', assigns: ['source' => ' ab c ']);
483-
assertTemplateResult(" \tab c", '{{ source | rstrip }}', assigns: ['source' => " \tab c \n \t"]);
482+
assertTemplateResult(' ab c', '{{ source | rstrip }}', staticData: ['source' => ' ab c ']);
483+
assertTemplateResult(" \tab c", '{{ source | rstrip }}', staticData: ['source' => " \tab c \n \t"]);
484484
});
485485

486486
test('strip new lines', function () {
487-
assertTemplateResult('abc', '{{ source | strip_newlines }}', assigns: ['source' => "a\nb\nc"]);
488-
assertTemplateResult('abc', '{{ source | strip_newlines }}', assigns: ['source' => "a\r\nb\nc"]);
487+
assertTemplateResult('abc', '{{ source | strip_newlines }}', staticData: ['source' => "a\nb\nc"]);
488+
assertTemplateResult('abc', '{{ source | strip_newlines }}', staticData: ['source' => "a\r\nb\nc"]);
489489
});
490490

491491
test('new lines to br', function () {
492-
assertTemplateResult("a<br />\nb<br />\nc", '{{ source | newline_to_br }}', assigns: ['source' => "a\nb\nc"]);
493-
assertTemplateResult("a<br />\nb<br />\nc", '{{ source | newline_to_br }}', assigns: ['source' => "a\r\nb\nc"]);
492+
assertTemplateResult("a<br />\nb<br />\nc", '{{ source | newline_to_br }}', staticData: ['source' => "a\nb\nc"]);
493+
assertTemplateResult("a<br />\nb<br />\nc", '{{ source | newline_to_br }}', staticData: ['source' => "a\r\nb\nc"]);
494494
});
495495

496496
test('plus', function () {
497497
assertTemplateResult('2', '{{ 1 | plus:1 }}');
498498
assertTemplateResult('2.1', "{{ '1' | plus:'1.1' }}");
499499

500-
assertTemplateResult('5', "{{ price | plus:'2' }}", assigns: ['price' => new NumberDrop(3)]);
500+
assertTemplateResult('5', "{{ price | plus:'2' }}", staticData: ['price' => new NumberDrop(3)]);
501501
});
502502

503503
test('minus', function () {
504504
assertTemplateResult('4', '{{ 5 | minus:1 }}');
505505
assertTemplateResult('2.3', "{{ '4.3' | minus:'2' }}");
506506

507-
assertTemplateResult('5', "{{ price | minus:'2' }}", assigns: ['price' => new NumberDrop(7)]);
507+
assertTemplateResult('5', "{{ price | minus:'2' }}", staticData: ['price' => new NumberDrop(7)]);
508508
});
509509

510510
test('abs', function () {
@@ -762,15 +762,15 @@
762762
test('sum without property calls to liquid', function () {
763763
$t = new ThingWithParamToLiquid;
764764

765-
renderTemplate('{{ foo | sum }}', assigns: ['foo' => [$t]]);
765+
renderTemplate('{{ foo | sum }}', staticData: ['foo' => [$t]]);
766766

767767
expect($t->value)->toBe(1);
768768
});
769769

770770
test('sum with property calls to liquid on property values', function () {
771771
$t = new ThingWithParamToLiquid;
772772

773-
renderTemplate('{{ foo | sum: "quantity" }}', assigns: ['foo' => [['quantity' => $t]]]);
773+
renderTemplate('{{ foo | sum: "quantity" }}', staticData: ['foo' => [['quantity' => $t]]]);
774774

775775
expect($t->value)->toBe(1);
776776
});

tests/Integration/StreamTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
$stream = streamTemplate(<<<'LIQUID'
2222
{{ var }}
2323
LIQUID,
24-
assigns: [
24+
staticData: [
2525
'var' => function () {
2626
yield 'text1';
2727
yield 'text2';
@@ -41,7 +41,7 @@
4141
$stream = streamTemplate(<<<'LIQUID'
4242
{{ var | join: ',' }}
4343
LIQUID,
44-
assigns: [
44+
staticData: [
4545
'var' => function () {
4646
yield 'text1';
4747
yield 'text2';

tests/Integration/Tags/AssignTagTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,21 +20,21 @@
2020
assertTemplateResult(
2121
'.foo.',
2222
'{% assign foo = values %}.{{ foo[0] }}.',
23-
assigns: ['values' => ['foo', 'bar', 'baz']]
23+
staticData: ['values' => ['foo', 'bar', 'baz']]
2424
);
2525

2626
assertTemplateResult(
2727
'.bar.',
2828
'{% assign foo = values %}.{{ foo[1] }}.',
29-
assigns: ['values' => ['foo', 'bar', 'baz']]
29+
staticData: ['values' => ['foo', 'bar', 'baz']]
3030
);
3131
});
3232

3333
test('assigned with filter', function () {
3434
assertTemplateResult(
3535
'.bar.',
3636
'{% assign foo = values | split: "," %}.{{ foo[1] }}.',
37-
assigns: ['values' => 'foo,bar,baz']
37+
staticData: ['values' => 'foo,bar,baz']
3838
);
3939
});
4040

@@ -50,7 +50,7 @@
5050
assertTemplateResult(
5151
'result',
5252
"{% assign r = a[ 'b' ] %}{{ r }}",
53-
assigns: ['a' => ['b' => 'result']]
53+
staticData: ['a' => ['b' => 'result']]
5454
);
5555
});
5656

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?php
22

33
test('echo outputs its input', function () {
4-
assertTemplateResult('BAR', '{%- echo variable-name | upcase -%}', assigns: ['variable-name' => 'bar']);
4+
assertTemplateResult('BAR', '{%- echo variable-name | upcase -%}', staticData: ['variable-name' => 'bar']);
55
});

0 commit comments

Comments
 (0)