@@ -77,13 +77,14 @@ public function isDev(array|ViteConfig|null $config = null): bool
7777 *
7878 * Backwards compatible with ViteScriptsHelper::script()
7979 *
80- * When `block` option is `false`, outputs tags directly ( inline) .
81- * Otherwise appends to the specified view block.
80+ * When `block` option is `false`, returns the tags as a string for inline output .
81+ * Otherwise appends to the specified view block and returns null .
8282 *
8383 * @param array<string, mixed>|string $options Options or file shorthand
8484 * @param \CakeVite\ValueObject\ViteConfig|array<string, mixed>|null $config Configuration
85+ * @return string|null Returns tag string when block is false, null otherwise
8586 */
86- public function script (array |string $ options = [], array |ViteConfig |null $ config = null ): void
87+ public function script (array |string $ options = [], array |ViteConfig |null $ config = null ): ? string
8788 {
8889 $ options = $ this ->normalizeOptions ($ options );
8990 $ config = $ this ->extractConfigFromOptions ($ options , $ config );
@@ -102,14 +103,16 @@ public function script(array|string $options = [], array|ViteConfig|null $config
102103
103104 $ tags = $ this ->getAssetService ()->generateScriptTags ($ config , $ options );
104105
106+ $ output = '' ;
107+
105108 // Render tags (preload tags as link elements, script tags as script elements)
106109 foreach ($ tags as $ tag ) {
107110 if ($ tag ->isPreload ) {
108111 // Render preload tags as <link rel="modulepreload" href="...">
109112 $ preloadType = $ tag ->preloadType ?? 'modulepreload ' ;
110113 $ linkTag = $ this ->buildPreloadLinkTag ($ preloadType , $ tag ->url , $ tag ->attributes );
111114 if ($ inline ) {
112- echo $ linkTag ;
115+ $ output .= $ linkTag ;
113116 } else {
114117 $ this ->getView ()->append ($ block , $ linkTag );
115118 }
@@ -118,29 +121,32 @@ public function script(array|string $options = [], array|ViteConfig|null $config
118121 // When block is false, Html::script() returns the tag string
119122 $ scriptTag = $ this ->Html ->script ($ tag ->url , array_merge (['block ' => $ block ], $ tag ->attributes ));
120123 if ($ inline && $ scriptTag !== null ) {
121- echo $ scriptTag ;
124+ $ output .= $ scriptTag ;
122125 }
123126 }
124127 }
125128
126129 // Add dependent CSS if in production
127130 if (!$ this ->isDev ($ config )) {
128- $ this ->addDependentCss ($ config , $ options , $ cssBlock , $ inline );
131+ $ output .= $ this ->addDependentCss ($ config , $ options , $ cssBlock , $ inline );
129132 }
133+
134+ return $ inline ? $ output : null ;
130135 }
131136
132137 /**
133138 * Render CSS tags
134139 *
135140 * Backwards compatible with ViteScriptsHelper::css()
136141 *
137- * When `block` option is `false`, outputs tags directly ( inline) .
138- * Otherwise appends to the specified view block.
142+ * When `block` option is `false`, returns the tags as a string for inline output .
143+ * Otherwise appends to the specified view block and returns null .
139144 *
140145 * @param array<string, mixed>|string $options Options or file shorthand
141146 * @param \CakeVite\ValueObject\ViteConfig|array<string, mixed>|null $config Configuration
147+ * @return string|null Returns tag string when block is false, null otherwise
142148 */
143- public function css (array |string $ options = [], array |ViteConfig |null $ config = null ): void
149+ public function css (array |string $ options = [], array |ViteConfig |null $ config = null ): ? string
144150 {
145151 $ options = $ this ->normalizeOptions ($ options );
146152 $ config = $ this ->extractConfigFromOptions ($ options , $ config );
@@ -152,13 +158,17 @@ public function css(array|string $options = [], array|ViteConfig|null $config =
152158
153159 $ tags = $ this ->getAssetService ()->generateStyleTags ($ config , $ options );
154160
161+ $ output = '' ;
162+
155163 foreach ($ tags as $ tag ) {
156164 // When block is false, Html::css() returns the tag string
157165 $ cssTag = $ this ->Html ->css ($ tag ->url , array_merge (['block ' => $ block ], $ tag ->attributes ));
158166 if ($ inline && $ cssTag !== null ) {
159- echo $ cssTag ;
167+ $ output .= $ cssTag ;
160168 }
161169 }
170+
171+ return $ inline ? $ output : null ;
162172 }
163173
164174 /**
@@ -293,10 +303,15 @@ private function extractConfigFromOptions(
293303 * @param \CakeVite\ValueObject\ViteConfig $config Configuration
294304 * @param array<string, mixed> $options Options
295305 * @param string|false $cssBlock CSS block name or false for inline output
296- * @param bool $inline Whether to output inline (when block is false)
306+ * @param bool $inline Whether to return inline (when block is false)
307+ * @return string Returns CSS tags when inline is true, empty string otherwise
297308 */
298- private function addDependentCss (ViteConfig $ config , array $ options , string |false $ cssBlock , bool $ inline = false ): void
299- {
309+ private function addDependentCss (
310+ ViteConfig $ config ,
311+ array $ options ,
312+ string |false $ cssBlock ,
313+ bool $ inline = false ,
314+ ): string {
300315 $ manifestService = new ManifestService ();
301316 $ manifest = $ manifestService ->load ($ config );
302317
@@ -308,14 +323,20 @@ private function addDependentCss(ViteConfig $config, array $options, string|fals
308323 $ entries = $ manifest ->filterEntries ();
309324 $ pluginPrefix = $ config ->pluginName ? $ config ->pluginName . '. ' : '' ;
310325
326+ $ output = '' ;
327+
311328 foreach ($ entries as $ entry ) {
312329 foreach ($ entry ->getDependentCssUrls () as $ cssUrl ) {
313- $ cssTag = $ this ->Html ->css ($ pluginPrefix . $ cssUrl , ['block ' => $ cssBlock ]);
330+ // When inline, pass block => false to get the tag string; otherwise use the block name
331+ $ blockOption = $ inline ? false : $ cssBlock ;
332+ $ cssTag = $ this ->Html ->css ($ pluginPrefix . $ cssUrl , ['block ' => $ blockOption ]);
314333 if ($ inline && $ cssTag !== null ) {
315- echo $ cssTag ;
334+ $ output .= $ cssTag ;
316335 }
317336 }
318337 }
338+
339+ return $ output ;
319340 }
320341
321342 /**
0 commit comments