diff --git a/src/View/Cascade.php b/src/View/Cascade.php index 85758fc158e..32dd9cc0eb5 100644 --- a/src/View/Cascade.php +++ b/src/View/Cascade.php @@ -158,6 +158,10 @@ protected function hydrateContent() return $this; } + if ($this->content instanceof \Closure) { + $this->content = call_user_func($this->content); + } + $variables = $this->content instanceof Augmentable ? $this->content->toDeferredAugmentedArray() : $this->content->toArray(); diff --git a/tests/View/CascadeTest.php b/tests/View/CascadeTest.php index 2b0c3e35b40..38da72797e5 100644 --- a/tests/View/CascadeTest.php +++ b/tests/View/CascadeTest.php @@ -410,6 +410,32 @@ public function it_hydrates_page_data() }); } + #[Test] + public function it_hydrates_page_data_by_closure() + { + $vars = ['foo' => 'bar', 'baz' => 'qux']; + $page = EntryFactory::id('test') + ->collection('example') + ->data($vars) + ->make(); + $cascade = $this->cascade()->withContent(fn () => $page); + + $this->assertEquals($page, call_user_func($cascade->content())); + + tap($cascade->hydrate()->toArray(), function ($cascade) use ($page) { + $this->assertArrayHasKey('page', $cascade); + $this->assertEquals($page, $cascade['page']); + + // The 'page' values should also be at the top level. + // They'll be Value classes so Antlers can lazily augment them. + // Blade can prefer {{ $globalhandle->field }} over just {{ $field }} + $this->assertEquals('bar', $cascade['foo']); + $this->assertEquals('qux', $cascade['baz']); + $this->assertInstanceOf(Value::class, $cascade['foo']); + $this->assertInstanceOf(Value::class, $cascade['baz']); + }); + } + #[Test] public function it_hydrates_globals() {