-
Notifications
You must be signed in to change notification settings - Fork 11.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[9.x] Discover anonymous Blade components in other folders #41637
[9.x] Discover anonymous Blade components in other folders #41637
Conversation
Do you have the ability to write tests for this? |
@taylorotwell Yes, if you're okay with the idea. |
I just added test coverage for this and applied the code style! |
I think one thing that gives me pause about this how it behaves when used via a package. I could see package maintainer's using anonymous components and wanting to use this feature, but how would they use it? It seems to always assume the "directory" is a dot path relative to the application's main view directory. |
@taylorotwell thank you for your comment! I just tested this with a package from myself and it works out of the box when a package author uses his usual Consider the following example, where I'm loading everything in the Blade::anonymousComponentNamespace('myPrefix', 'media-library::media'); {{-- Classic, regular way: --}}
@include('media-library::media.empty-state')
{{-- New way, if authors choose for it --}}
<x-myPrefix::empty-state></x-myPrefix::empty-state>
{{-- Outputs the same component twice --}} The point is that is doesn't really assume a view relative to the application's view directory. It mainly tries to construct a correct view name, whether or not it contains the We're just replacing |
Thanks! I decided to swap the order of directory and prefix - I felt that was a bit more consistent with the componentNamespace method which accepts "location" first and then "prefix". |
Do you like anonymous components? (I like them a lot) And did you ever want to also 'enable' anonymous components for directories other than
resources/views/components
?For example,
resources/views/admin
andresources/view/frontend
? And autodiscover the components like<x-admin::component.name
?This PR introduces exactly that. It allows the developer to register a directory and a prefix, which would allow to use the files in those directories as anonymous components. This would get rid of things like
@include
and provide developers with the ability to use the attribute bag everywhere.Example
In your service provider, you can register the directories you want to enable discovery for:
Now you can use this in your Blade files like this:
I added some additional inline comments to the compiler-file just for clarity, but feel free to trim this down however you want.
I took inspiration from the original PR that introduced anonymous components (#31363). I'm a slightly unsure how this should be tested, perhaps @driesvints could give me a hint?
This is a feature I wanted for a long time and I hope this will be useful to many developers! The syntax is in line with package components.