Skip to content

Commit

Permalink
support nested variable lookup
Browse files Browse the repository at this point in the history
  • Loading branch information
cappuc committed Aug 21, 2024
1 parent 1b5763d commit 23010e4
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/Nodes/VariableLookup.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ public function evaluate(RenderContext $context): mixed
}

foreach ($variables as $object) {
$object = $context->evaluate($object);

if ($object instanceof \Generator) {
$object = iterator_to_array($object, preserve_keys: false);
}
Expand All @@ -102,7 +104,7 @@ public function evaluate(RenderContext $context): mixed

assert(is_string($key) || is_int($key));

$nextObject = $context->internalContextLookup($object, $key);
$nextObject = $context->evaluate($context->internalContextLookup($object, $key));

if ($nextObject instanceof MissingValue && is_iterable($object) && in_array($i, $this->lookupFilters)) {
$nextObject = $context->applyFilter($lookup, $object);
Expand Down
12 changes: 12 additions & 0 deletions tests/Integration/VariableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,18 @@
], factory: $templateFactory);
});

test('nested variable lookup', function () {
assertTemplateResult('1', '{{ c.b }}', [
'a' => ['b' => 1],
'c' => new \Keepsuit\Liquid\Nodes\VariableLookup('a'),
]);

assertTemplateResult('1', '{{ c.d.b }}', [
'a' => ['b' => 1],
'c' => ['d' => new \Keepsuit\Liquid\Nodes\VariableLookup('a')],
]);
});

function generator(): Generator
{
yield '1';
Expand Down

0 comments on commit 23010e4

Please sign in to comment.