Skip to content

Adds fragments() and fragmentsIf() method#45669

Merged
taylorotwell merged 3 commits into
laravel:9.xfrom
arkoe:multiple_fragments
Jan 16, 2023
Merged

Adds fragments() and fragmentsIf() method#45669
taylorotwell merged 3 commits into
laravel:9.xfrom
arkoe:multiple_fragments

Conversation

@arkoe

@arkoe arkoe commented Jan 16, 2023

Copy link
Copy Markdown
Contributor

Adds fragments() and fragmentsIf() function.
With htmx (and probably similar libraries) you can swap out of bounds, allowing you to update multiple sections of a page, requiring multiple fragments. The current way to do this is by concatenating two (or more) fragments:

return view('welcome')->fragment('fragment1') . view('welcome')->fragment('fragment2');

With fragments() this is simplified to:

return view('welcome')->fragments(['fragment1', 'fragment2']);

or

return view('welcome')->fragments('fragment1', 'fragment2');

Also a fragmentsIf() to conditionally return these fragments have been added in this PR. This is basically an expansion on PR #45656, allowing you to conditionally return multiple fragments:

return view('welcome')->fragmentsIf(request()->hasHeader('HX-Request'), ['fragment1', 'fragment2']);

or

return view('welcome')->fragmentsIf(request()->hasHeader('HX-Request'), 'fragment1', 'fragment2');

@taylorotwell taylorotwell merged commit 31fa534 into laravel:9.x Jan 16, 2023
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.

2 participants