Open godismyjudge95 opened 3 years ago
Keen for this. What would we want the API to look like? I'm not sure it warrants a whole new helper, but maybe a new method on the Illuminate\View\Factory
class, so it could be used like this:
public function getIconAttribute()
{
return view()->component('icon', ['name' => $this->icon_name]);
}
This new Illuminate\View\Factory::component($name, $attributes, $slots)
method would ultimately return a string via the existing renderComponent
method.
Sometimes one might have need of rendering a blade component directly from say a controller or maybe a customized component instance from a model method.
For example:
This works for simpler components; however, if the component looks like the following, it fails:
The reason why this fails is because the
$attributes
variable is never instantiated when the component is rendered directly. To get around this one might just pass an attribute variable from the view method, however this also fails due to the attributes variable needing to be an instance ofComponentAttributeBag
.My current solution - albeit a poor one - is to have a helper method instantiate an
AnonymousComponent
and then proceed to render that:It would be nice if there was a built in solution to this use case and one that also allowed for rendering of class based components.
I realize this might be a really niche case, but I have not found any other way to re-use the code from the blade components outside a view file.