From 94e4f15ff4ef95672dc534fec117364fe062a77b Mon Sep 17 00:00:00 2001 From: mscherer Date: Sun, 8 Mar 2026 08:44:44 +0100 Subject: [PATCH 1/4] Add support for inline output with block => false When passing `block => false` option to script() or css() methods, the helper now outputs tags directly instead of buffering to view blocks. This is consistent with CakePHP's Html helper behavior and allows for inline asset rendering in templates without using view blocks. Changes: - script(): Echo output when block is false, handle preload tags inline - css(): Echo output when block is false - addDependentCss(): Support inline parameter for dependent CSS Fixes TypeError when passing block => false (previously crashed with "Argument #1 ($name) must be of type string, false given"). --- src/View/Helper/ViteHelper.php | 38 ++++- tests/TestCase/View/Helper/ViteHelperTest.php | 143 ++++++++++++++++++ 2 files changed, 174 insertions(+), 7 deletions(-) diff --git a/src/View/Helper/ViteHelper.php b/src/View/Helper/ViteHelper.php index 52932ec..410fcc9 100644 --- a/src/View/Helper/ViteHelper.php +++ b/src/View/Helper/ViteHelper.php @@ -76,6 +76,9 @@ public function isDev(array|ViteConfig|null $config = null): bool * * Backwards compatible with ViteScriptsHelper::script() * + * When `block` option is `false`, outputs tags directly (inline). + * Otherwise appends to the specified view block. + * * @param array|string $options Options or file shorthand * @param \CakeVite\ValueObject\ViteConfig|array|null $config Configuration */ @@ -94,6 +97,7 @@ public function script(array|string $options = [], array|ViteConfig|null $config $block = $options['block'] ?? $config->scriptBlock; $cssBlock = $options['cssBlock'] ?? $config->cssBlock; + $inline = $block === false; $tags = $this->getAssetService()->generateScriptTags($config, $options); @@ -103,16 +107,24 @@ public function script(array|string $options = [], array|ViteConfig|null $config // Render preload tags as $preloadType = $tag->preloadType ?? 'modulepreload'; $linkTag = $this->buildPreloadLinkTag($preloadType, $tag->url, $tag->attributes); - $this->getView()->append($block, $linkTag); + if ($inline) { + echo $linkTag; + } else { + $this->getView()->append($block, $linkTag); + } } else { // Render regular script tags - $this->Html->script($tag->url, array_merge(['block' => $block], $tag->attributes)); + // When block is false, Html::script() returns the tag string + $scriptTag = $this->Html->script($tag->url, array_merge(['block' => $block], $tag->attributes)); + if ($inline && $scriptTag !== null) { + echo $scriptTag; + } } } // Add dependent CSS if in production if (!$this->isDev($config)) { - $this->addDependentCss($config, $options, $cssBlock); + $this->addDependentCss($config, $options, $cssBlock, $inline); } } @@ -121,6 +133,9 @@ public function script(array|string $options = [], array|ViteConfig|null $config * * Backwards compatible with ViteScriptsHelper::css() * + * When `block` option is `false`, outputs tags directly (inline). + * Otherwise appends to the specified view block. + * * @param array|string $options Options or file shorthand * @param \CakeVite\ValueObject\ViteConfig|array|null $config Configuration */ @@ -132,11 +147,16 @@ public function css(array|string $options = [], array|ViteConfig|null $config = $config = $this->resolveConfig($config); $block = $options['block'] ?? $config->cssBlock; + $inline = $block === false; $tags = $this->getAssetService()->generateStyleTags($config, $options); foreach ($tags as $tag) { - $this->Html->css($tag->url, array_merge(['block' => $block], $tag->attributes)); + // When block is false, Html::css() returns the tag string + $cssTag = $this->Html->css($tag->url, array_merge(['block' => $block], $tag->attributes)); + if ($inline && $cssTag !== null) { + echo $cssTag; + } } } @@ -271,9 +291,10 @@ private function extractConfigFromOptions( * * @param \CakeVite\ValueObject\ViteConfig $config Configuration * @param array $options Options - * @param string $cssBlock CSS block name + * @param string|false $cssBlock CSS block name or false for inline output + * @param bool $inline Whether to output inline (when block is false) */ - private function addDependentCss(ViteConfig $config, array $options, string $cssBlock): void + private function addDependentCss(ViteConfig $config, array $options, string|false $cssBlock, bool $inline = false): void { $manifestService = new ManifestService(); $manifest = $manifestService->load($config); @@ -288,7 +309,10 @@ private function addDependentCss(ViteConfig $config, array $options, string $css foreach ($entries as $entry) { foreach ($entry->getDependentCssUrls() as $cssUrl) { - $this->Html->css($pluginPrefix . $cssUrl, ['block' => $cssBlock]); + $cssTag = $this->Html->css($pluginPrefix . $cssUrl, ['block' => $cssBlock]); + if ($inline && $cssTag !== null) { + echo $cssTag; + } } } } diff --git a/tests/TestCase/View/Helper/ViteHelperTest.php b/tests/TestCase/View/Helper/ViteHelperTest.php index f9c806e..4669ab7 100644 --- a/tests/TestCase/View/Helper/ViteHelperTest.php +++ b/tests/TestCase/View/Helper/ViteHelperTest.php @@ -727,4 +727,147 @@ public function testBuildPreloadLinkTagEscapesSpecialCharacters(): void $this->assertStringNotContainsString('