Skip to content

Commit f218b96

Browse files
committed
feat: add region metadata support to Template fluent API
1 parent ceec3a4 commit f218b96

2 files changed

Lines changed: 86 additions & 0 deletions

File tree

packages/core/src/Data/Template.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ class Template
1111

1212
protected ?array $order = null;
1313

14+
protected ?string $regionId = null;
15+
16+
protected ?string $regionName = null;
17+
1418
/**
1519
* Create a new template.
1620
*/
@@ -52,6 +56,26 @@ public function order(array $order): static
5256
return $this;
5357
}
5458

59+
/**
60+
* Set region ID (for single-region templates).
61+
*/
62+
public function id(string $id): static
63+
{
64+
$this->regionId = $id;
65+
66+
return $this;
67+
}
68+
69+
/**
70+
* Set region name (for single-region templates).
71+
*/
72+
public function name(string $name): static
73+
{
74+
$this->regionName = $name;
75+
76+
return $this;
77+
}
78+
5579
/**
5680
* Convert to array.
5781
*/
@@ -68,6 +92,18 @@ public function toArray(): array
6892
if ($this->order !== null) {
6993
$data['order'] = $this->order;
7094
}
95+
96+
if ($this->regionId !== null) {
97+
$blockIds = $this->order ?? array_keys($data['blocks']);
98+
99+
$data['regions'] = [
100+
[
101+
'id' => $this->regionId,
102+
'name' => $this->regionName ?? $this->regionId,
103+
'blocks' => $blockIds,
104+
],
105+
];
106+
}
71107
}
72108

73109
return $data;

tests/laravel/Unit/PhpTemplateTest.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -379,3 +379,53 @@ protected function build(): void
379379
expect($template['blocks']['grid']['children'][0]['properties']['featured'])->toBeTrue();
380380
});
381381
});
382+
383+
describe('Template region API', function () {
384+
it('supports region id and name', function () {
385+
$template = Template::make()
386+
->id('header')
387+
->name('Header')
388+
->block('logo', 'logo')
389+
->block('nav', 'navigation')
390+
->toArray();
391+
392+
expect($template['blocks'])->toHaveCount(2);
393+
expect($template['regions'])->toHaveCount(1);
394+
expect($template['regions'][0]['id'])->toBe('header');
395+
expect($template['regions'][0]['name'])->toBe('Header');
396+
expect($template['regions'][0]['blocks'])->toBe(['logo', 'nav']);
397+
});
398+
399+
it('uses region id as name when name not provided', function () {
400+
$template = Template::make()
401+
->id('footer')
402+
->block('copyright', 'text')
403+
->toArray();
404+
405+
expect($template['regions'][0]['id'])->toBe('footer');
406+
expect($template['regions'][0]['name'])->toBe('footer');
407+
expect($template['regions'][0]['blocks'])->toBe(['copyright']);
408+
});
409+
410+
it('respects block order in region', function () {
411+
$template = Template::make()
412+
->id('header')
413+
->name('Header')
414+
->block('logo', 'logo')
415+
->block('nav', 'navigation')
416+
->block('search', 'search')
417+
->order(['nav', 'search', 'logo'])
418+
->toArray();
419+
420+
expect($template['regions'][0]['blocks'])->toBe(['nav', 'search', 'logo']);
421+
expect($template['order'])->toBe(['nav', 'search', 'logo']);
422+
});
423+
424+
it('does not include regions when region id not set', function () {
425+
$template = Template::make()
426+
->block('hero', 'hero')
427+
->toArray();
428+
429+
expect($template)->not()->toHaveKey('regions');
430+
});
431+
});

0 commit comments

Comments
 (0)