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
4 changes: 4 additions & 0 deletions src/View/Cascade.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
26 changes: 26 additions & 0 deletions tests/View/CascadeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down