Skip to content

Add support for inline output with block => false#2

Merged
josbeir merged 5 commits intojosbeir:mainfrom
dereuromark:fix-inline-output-support
Mar 8, 2026
Merged

Add support for inline output with block => false#2
josbeir merged 5 commits intojosbeir:mainfrom
dereuromark:fix-inline-output-support

Conversation

@dereuromark
Copy link
Copy Markdown
Contributor

@dereuromark dereuromark commented Mar 8, 2026

Summary

When passing block => false option to script() or css() methods, the helper now returns the tags as a string instead of buffering to view blocks.

This is consistent with CakePHP's Html helper behavior and allows for inline asset rendering in templates.

Bug fixed: Previously passing block => false would crash with:

TypeError: Cake\View\View::append(): Argument #1 ($name) must be of type string, false given

Changes

  • script(): Returns ?string - tag string when block is false, null otherwise
  • css(): Returns ?string - tag string when block is false, null otherwise
  • addDependentCss(): Returns dependent CSS tags when inline mode is enabled

Use Case

This enables inline asset loading in view elements:

// In a template element - outputs directly without view blocks
<?= $this->Vite->css(['files' => ['theme'], 'block' => false]) ?>
<?= $this->Vite->script(['files' => ['app'], 'block' => false]) ?>

Tests

  • testScriptWithBlockFalseOutputsInline
  • testScriptWithBlockFalseInProductionMode
  • testCssWithBlockFalseOutputsInline
  • testCssWithBlockFalseInProductionMode
  • testScriptWithBlockFalseOutputsPreloadTagsInline
  • testScriptWithBlockFalseOutputsDependentCssInline

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").
@josbeir
Copy link
Copy Markdown
Owner

josbeir commented Mar 8, 2026

thanks!
could you merge my ci fixes from the main branch so we know CI is green ?

There also seem to be some other test failures that should be handled ?

@dereuromark
Copy link
Copy Markdown
Contributor Author

You need to allow this in the settings. Then i can click the button :)

@josbeir
Copy link
Copy Markdown
Owner

josbeir commented Mar 8, 2026

You need to allow this in the settings. Then i can click the button :)

It should be good for new work by you. It seems that this setting is for "new contributors to the project"

@dereuromark
Copy link
Copy Markdown
Contributor Author

Allow updating in PR or sth
It is its own setting

@josbeir
Copy link
Copy Markdown
Owner

josbeir commented Mar 8, 2026

Allow updating in PR or sth It is its own setting

think its okay how it is configured now. It should start the worlflow automatically for new commits on this branch

@dereuromark
Copy link
Copy Markdown
Contributor Author

Thats a different setting. The updste Button is a different one. Its not the one about permission.

@dereuromark
Copy link
Copy Markdown
Contributor Author

Screenshot_20260308_130211_Chrome

@josbeir
Copy link
Copy Markdown
Owner

josbeir commented Mar 8, 2026

Right, done!

josbeir and others added 2 commits March 8, 2026 13:38
Change script() and css() methods to return the tag string when
block => false, instead of echoing directly. This is more consistent
with CakePHP's HtmlHelper conventions and makes the code easier to test.
@dereuromark dereuromark force-pushed the fix-inline-output-support branch from 9f6c867 to 675a3e0 Compare March 8, 2026 14:59
@dereuromark
Copy link
Copy Markdown
Contributor Author

I cleaned up the approach. Now it looks good.

@josbeir
Copy link
Copy Markdown
Owner

josbeir commented Mar 8, 2026

maybe just add a little entry about $block in the docs?

@josbeir
Copy link
Copy Markdown
Owner

josbeir commented Mar 8, 2026

The actual issue I see is in development mode: every script() call injects @vite/client, so multiple calls in one request can output the client tag multiple times (layout + element, multiple elements, etc.).
This PR keeps that behavior and makes it more visible in inline mode.

Would you consider a small follow-up to deduplicate @vite/client per request?

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds inline rendering support to the CakeVite ViteHelper so callers can pass block => false to get generated <script>/<link> tags back as a string (instead of appending to view blocks), aligning behavior with CakePHP’s HtmlHelper and fixing the prior View::append(false, ...) crash.

Changes:

  • Update ViteHelper::script() and ViteHelper::css() to return ?string (string when block === false, otherwise null).
  • Update dependent CSS handling so it can be returned inline when inline mode is enabled.
  • Add README documentation and PHPUnit coverage for inline output scenarios (dev + production, preload tags, dependent CSS).

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

File Description
src/View/Helper/ViteHelper.php Implements inline output return values for script()/css() and adjusts dependent CSS handling to support inline mode.
tests/TestCase/View/Helper/ViteHelperTest.php Adds tests asserting inline output is returned and that view blocks remain empty when block => false.
README.md Documents the new “Inline Output” behavior and adds it to the table of contents.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

…ge cases

- Add viteClientRendered tracking to ViteHelper to prevent duplicate @vite/client output
- Update AssetService to support skipViteClient option
- Fix pluginScript/pluginCss to return ?string for inline mode (block => false)
- Fix addDependentCss to skip CSS when cssBlock is false and not in inline mode
- Add tests for deduplication and plugin inline output
- Document deduplication and cssBlock behavior in README
@dereuromark
Copy link
Copy Markdown
Contributor Author

Done! Added the @vite/client deduplication to this PR along with docs updates.

Changes:

  • @vite/client is now only output once per request in development mode, even with multiple script() calls
  • pluginScript()/pluginCss() now return ?string to support inline mode
  • cssBlock => false now properly skips dependent CSS injection when not in inline mode
  • Updated README with docs for deduplication and cssBlock option

@josbeir josbeir merged commit 8603841 into josbeir:main Mar 8, 2026
6 checks passed
@josbeir
Copy link
Copy Markdown
Owner

josbeir commented Mar 8, 2026

thanks!

@dereuromark dereuromark deleted the fix-inline-output-support branch March 8, 2026 16:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants