From 23010e4234b4f18bdc456215865ef22aa0e517b6 Mon Sep 17 00:00:00 2001 From: Fabio Capucci Date: Wed, 21 Aug 2024 16:17:36 +0200 Subject: [PATCH] support nested variable lookup --- src/Nodes/VariableLookup.php | 4 +++- tests/Integration/VariableTest.php | 12 ++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/src/Nodes/VariableLookup.php b/src/Nodes/VariableLookup.php index c32928e..156cae6 100644 --- a/src/Nodes/VariableLookup.php +++ b/src/Nodes/VariableLookup.php @@ -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); } @@ -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); diff --git a/tests/Integration/VariableTest.php b/tests/Integration/VariableTest.php index e6c7cff..e7cb142 100644 --- a/tests/Integration/VariableTest.php +++ b/tests/Integration/VariableTest.php @@ -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';