Skip to content
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
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions src/Tags/RenderTag.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ public function stream(RenderContext $context): \Generator
$forLoop = new ForLoopDrop($templateName, count($variable));

foreach ($variable as $value) {
$partialContext = $this->setInnerContextVariables($context->newIsolatedSubContext($templateName), [
$partialContext = $this->buildPartialContext($context, $templateName, [
'forloop' => $forLoop,
$contextVariableName => $value,
]);
Expand All @@ -132,7 +132,7 @@ public function stream(RenderContext $context): \Generator
return;
}

$partialContext = $this->setInnerContextVariables($context->newIsolatedSubContext($templateName), [
$partialContext = $this->buildPartialContext($context, $templateName, [
$contextVariableName => $variable,
]);

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

protected function setInnerContextVariables(RenderContext $context, array $variables = []): RenderContext
protected function buildPartialContext(RenderContext $rootContext, string $templateName, array $variables = []): RenderContext
{
$partialContext = $rootContext->newIsolatedSubContext($templateName);

foreach ($variables as $key => $value) {
$context->set($key, $value);
$partialContext->set($key, $value);
}

foreach ($this->attributes as $key => $value) {
$context->set($key, $context->evaluate($value));
$partialContext->set($key, $rootContext->evaluate($value));
}

return $context;
return $partialContext;
}

protected function allowDynamicPartials(): bool
Expand Down
12 changes: 6 additions & 6 deletions tests/Integration/CustomFiltersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@

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

expect(renderTemplate('{{ false | ternary: "yes", "no" }}', factory: $this->factory))->toBe('no');
expect(renderTemplate('{{ test | ternary: "yes", "no" }}', factory: $this->factory, assigns: ['test' => []]))->toBe('no');
expect(renderTemplate('{{ test | ternary: "yes", "no" }}', factory: $this->factory, assigns: ['test' => '']))->toBe('no');
expect(renderTemplate('{{ test | ternary: "yes", "no" }}', factory: $this->factory, assigns: ['test' => null]))->toBe('no');
expect(renderTemplate('{{ test | ternary: "yes", "no" }}', factory: $this->factory, staticData: ['test' => []]))->toBe('no');
expect(renderTemplate('{{ test | ternary: "yes", "no" }}', factory: $this->factory, staticData: ['test' => '']))->toBe('no');
expect(renderTemplate('{{ test | ternary: "yes", "no" }}', factory: $this->factory, staticData: ['test' => null]))->toBe('no');
});
2 changes: 1 addition & 1 deletion tests/Integration/ErrorHandlingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
Bla.
HTML;

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

test('standard error', function () {
Expand Down
50 changes: 25 additions & 25 deletions tests/Integration/StandardFilterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@
assertTemplateResult(
'{test=>1234}',
'{{ foo | map: "registers" }}',
assigns: [
staticData: [
'foo' => $model,
],
registers: [
Expand All @@ -280,15 +280,15 @@
assertTemplateResult(
'4217',
'{{ thing | map: "foo" | map: "bar" }}',
assigns: ['thing' => ['foo' => [['bar' => 42], ['bar' => 17]]]]
staticData: ['thing' => ['foo' => [['bar' => 42], ['bar' => 17]]]]
);
});

test('legacy map on hashes with dynamic key', function () {
assertTemplateResult(
'42',
'{% assign key = \'foo\' %}{{ thing | map: key | map: \'bar\' }}',
assigns: ['thing' => ['foo' => ['bar' => 42]]]
staticData: ['thing' => ['foo' => ['bar' => 42]]]
);
});

Expand All @@ -298,7 +298,7 @@
assertTemplateResult(
'woot: 1',
'{{ foo | sort: "whatever" }}',
assigns: ['foo' => [$t]]
staticData: ['foo' => [$t]]
);

expect($t->value)->toBe(1);
Expand All @@ -311,7 +311,7 @@
assertTemplateResult(
'testfoo',
'{{ closures | map: "value" }}',
assigns: ['closures' => [$c]]
staticData: ['closures' => [$c]]
);
});

Expand All @@ -324,15 +324,15 @@
assertTemplateResult(
'foobar',
'{{ drops | map: "closure" }}',
assigns: ['drops' => $drops]
staticData: ['drops' => $drops]
);
});

test('map works on iterator', function () {
assertTemplateResult(
'123',
'{{ foo | map: "foo" }}',
assigns: ['foo' => new \Keepsuit\Liquid\Tests\Stubs\IteratorDrop]
staticData: ['foo' => new \Keepsuit\Liquid\Tests\Stubs\IteratorDrop]
);
});

Expand Down Expand Up @@ -360,28 +360,28 @@
assertTemplateResult(
'213',
'{{ foo | sort: "bar" | map: "foo" }}',
assigns: ['foo' => new \Keepsuit\Liquid\Tests\Stubs\IteratorDrop]
staticData: ['foo' => new \Keepsuit\Liquid\Tests\Stubs\IteratorDrop]
);
});

test('first and last calls toLiquid', function () {
assertTemplateResult(
'foobar',
'{{ foo | first }}',
assigns: ['foo' => [new \Keepsuit\Liquid\Tests\Stubs\ThingWithToLiquid]]
staticData: ['foo' => [new \Keepsuit\Liquid\Tests\Stubs\ThingWithToLiquid]]
);
assertTemplateResult(
'foobar',
'{{ foo | last }}',
assigns: ['foo' => [new \Keepsuit\Liquid\Tests\Stubs\ThingWithToLiquid]]
staticData: ['foo' => [new \Keepsuit\Liquid\Tests\Stubs\ThingWithToLiquid]]
);
});

test('truncate calls toLiquid', function () {
assertTemplateResult(
'wo...',
'{{ foo | truncate: 5 }}',
assigns: ['foo' => new ThingWithParamToLiquid]
staticData: ['foo' => new ThingWithParamToLiquid]
);
});

Expand Down Expand Up @@ -469,42 +469,42 @@
});

test('strip', function () {
assertTemplateResult('ab c', '{{ source | strip }}', assigns: ['source' => ' ab c ']);
assertTemplateResult('ab c', '{{ source | strip }}', assigns: ['source' => " \tab c \n \t"]);
assertTemplateResult('ab c', '{{ source | strip }}', staticData: ['source' => ' ab c ']);
assertTemplateResult('ab c', '{{ source | strip }}', staticData: ['source' => " \tab c \n \t"]);
});

test('lstrip', function () {
assertTemplateResult('ab c ', '{{ source | lstrip }}', assigns: ['source' => ' ab c ']);
assertTemplateResult("ab c \n \t", '{{ source | lstrip }}', assigns: ['source' => " \tab c \n \t"]);
assertTemplateResult('ab c ', '{{ source | lstrip }}', staticData: ['source' => ' ab c ']);
assertTemplateResult("ab c \n \t", '{{ source | lstrip }}', staticData: ['source' => " \tab c \n \t"]);
});

test('rstrip', function () {
assertTemplateResult(' ab c', '{{ source | rstrip }}', assigns: ['source' => ' ab c ']);
assertTemplateResult(" \tab c", '{{ source | rstrip }}', assigns: ['source' => " \tab c \n \t"]);
assertTemplateResult(' ab c', '{{ source | rstrip }}', staticData: ['source' => ' ab c ']);
assertTemplateResult(" \tab c", '{{ source | rstrip }}', staticData: ['source' => " \tab c \n \t"]);
});

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

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

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

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

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

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

test('abs', function () {
Expand Down Expand Up @@ -762,15 +762,15 @@
test('sum without property calls to liquid', function () {
$t = new ThingWithParamToLiquid;

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

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

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

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

expect($t->value)->toBe(1);
});
4 changes: 2 additions & 2 deletions tests/Integration/StreamTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
$stream = streamTemplate(<<<'LIQUID'
{{ var }}
LIQUID,
assigns: [
staticData: [
'var' => function () {
yield 'text1';
yield 'text2';
Expand All @@ -41,7 +41,7 @@
$stream = streamTemplate(<<<'LIQUID'
{{ var | join: ',' }}
LIQUID,
assigns: [
staticData: [
'var' => function () {
yield 'text1';
yield 'text2';
Expand Down
8 changes: 4 additions & 4 deletions tests/Integration/Tags/AssignTagTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,21 @@
assertTemplateResult(
'.foo.',
'{% assign foo = values %}.{{ foo[0] }}.',
assigns: ['values' => ['foo', 'bar', 'baz']]
staticData: ['values' => ['foo', 'bar', 'baz']]
);

assertTemplateResult(
'.bar.',
'{% assign foo = values %}.{{ foo[1] }}.',
assigns: ['values' => ['foo', 'bar', 'baz']]
staticData: ['values' => ['foo', 'bar', 'baz']]
);
});

test('assigned with filter', function () {
assertTemplateResult(
'.bar.',
'{% assign foo = values | split: "," %}.{{ foo[1] }}.',
assigns: ['values' => 'foo,bar,baz']
staticData: ['values' => 'foo,bar,baz']
);
});

Expand All @@ -50,7 +50,7 @@
assertTemplateResult(
'result',
"{% assign r = a[ 'b' ] %}{{ r }}",
assigns: ['a' => ['b' => 'result']]
staticData: ['a' => ['b' => 'result']]
);
});

Expand Down
2 changes: 1 addition & 1 deletion tests/Integration/Tags/EchoTagTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php

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