From f159178dbe66b10b23966cca7794585a943abd02 Mon Sep 17 00:00:00 2001 From: Emmanuel Joseph Beron Date: Wed, 1 Oct 2025 09:04:53 +0800 Subject: [PATCH 1/5] feat: allow setting of template data --- src/Html/Builder.php | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/src/Html/Builder.php b/src/Html/Builder.php index 9172874..7112e50 100644 --- a/src/Html/Builder.php +++ b/src/Html/Builder.php @@ -62,6 +62,8 @@ class Builder protected array $additionalScripts = []; + protected array $templateData = []; + public function __construct(public Repository $config, public Factory $view, public HtmlBuilder $html) { /** @var array $defaults */ @@ -158,7 +160,13 @@ protected function template(): string $template = $this->template ?: $configTemplate; - return $this->view->make($template, ['editors' => $this->editors, 'scripts' => $this->additionalScripts])->render(); + return $this->view->make( + $template, + array_merge( + ['editors' => $this->editors, 'scripts' => $this->additionalScripts], + $this->templateData, + ) + )->render(); } /** @@ -263,4 +271,15 @@ public function addScript(string $view): static return $this; } + + public function templateData(array|\Closure $data = []): static + { + if ($data instanceof \Closure) { + $data = $data($this) ?? []; + } + + $this->templateData = $data; + + return $this; + } } From 1161adc4d9cb8bf62b54e3b73367784248ad2997 Mon Sep 17 00:00:00 2001 From: Emmanuel Joseph Beron Date: Wed, 1 Oct 2025 09:09:14 +0800 Subject: [PATCH 2/5] refactor: rename templateData method to setTemplateData --- src/Html/Builder.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Html/Builder.php b/src/Html/Builder.php index 7112e50..2686a78 100644 --- a/src/Html/Builder.php +++ b/src/Html/Builder.php @@ -272,7 +272,7 @@ public function addScript(string $view): static return $this; } - public function templateData(array|\Closure $data = []): static + public function setTemplateData(array|\Closure $data = []): static { if ($data instanceof \Closure) { $data = $data($this) ?? []; From 19f1828cc9baba491f84a311a8a7b1f4564698d5 Mon Sep 17 00:00:00 2001 From: Emmanuel Joseph Beron Date: Wed, 1 Oct 2025 09:26:58 +0800 Subject: [PATCH 3/5] test: add unit test for setting template data --- tests/Html/Builder/BuilderTest.php | 28 +++++++++++++++++++ .../test-builder-script.blade.php | 1 + 2 files changed, 29 insertions(+) create mode 100644 tests/TestComponents/test-builder-script.blade.php diff --git a/tests/Html/Builder/BuilderTest.php b/tests/Html/Builder/BuilderTest.php index 1b61ef2..897efb4 100644 --- a/tests/Html/Builder/BuilderTest.php +++ b/tests/Html/Builder/BuilderTest.php @@ -303,4 +303,32 @@ public function it_ignores_unauthorized_columns(): void $this->assertCount(1, $builder->getColumns()); } + + #[Test] + public function it_can_set_template_data(): void + { + $builder = $this->getHtmlBuilder() + ->addScript('test-builder-script') + ->setTemplateData(['message' => 'Test Message']); + + $this->assertStringContainsString( + "console.log({ tableId: 'noneset', message: 'Test Message' });", + $builder->generateScripts()->toHtml() + ); + + $builder + ->setTableId('my-table') + ->addScript('test-builder-script') + ->setTemplateData(function (Builder $builder): array { + return [ + 'tableId' => $builder->getTableId(), + 'message' => 'Set Template Data Using Callback', + ]; + }); + + $this->assertStringContainsString( + "console.log({ tableId: 'my-table', message: 'Set Template Data Using Callback' });", + $builder->generateScripts()->toHtml() + ); + } } diff --git a/tests/TestComponents/test-builder-script.blade.php b/tests/TestComponents/test-builder-script.blade.php new file mode 100644 index 0000000..a3ef460 --- /dev/null +++ b/tests/TestComponents/test-builder-script.blade.php @@ -0,0 +1 @@ +console.log({ tableId: @js($tableId ?? 'noneset'), message: @js($message) }); From 47f4e660dc329f7b27cfc6cf324dfa1b20a41337 Mon Sep 17 00:00:00 2001 From: Emmanuel Joseph Beron Date: Wed, 1 Oct 2025 09:32:48 +0800 Subject: [PATCH 4/5] refactor: remove redundant script addition in BuilderTest --- tests/Html/Builder/BuilderTest.php | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/Html/Builder/BuilderTest.php b/tests/Html/Builder/BuilderTest.php index 897efb4..32071e8 100644 --- a/tests/Html/Builder/BuilderTest.php +++ b/tests/Html/Builder/BuilderTest.php @@ -318,7 +318,6 @@ public function it_can_set_template_data(): void $builder ->setTableId('my-table') - ->addScript('test-builder-script') ->setTemplateData(function (Builder $builder): array { return [ 'tableId' => $builder->getTableId(), From bbfacfd8e87c0aba164352bbd38d447a17360dcb Mon Sep 17 00:00:00 2001 From: Emmanuel Joseph Beron Date: Wed, 1 Oct 2025 10:08:02 +0800 Subject: [PATCH 5/5] chore: pint --- src/Html/Builder.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Html/Builder.php b/src/Html/Builder.php index 03e80e0..95e3454 100644 --- a/src/Html/Builder.php +++ b/src/Html/Builder.php @@ -276,7 +276,7 @@ public function getTemplate(): string { return $this->template; } - + public function setTemplateData(array|\Closure $data = []): static { if ($data instanceof \Closure) {