Please add support for component classes for Blade components.
You can register class-based Blade components in Jigsaw with $bladeCompiler->component(). See #440.
This basic example in my blade.php does not work. I have not created a view for it yet. I use <x-icon /> in my view.
use Illuminate\View\Component;
class Icon extends Component
{
public function render()
{
return "Icon works!";
}
}
$bladeCompiler->component(Icon::class);
This code results in an error when building the site:
In 05187dc394c0a6d7c726c66dad2543a3097e1c8e.php line 22:
syntax error, unexpected 'endif' (T_ENDIF), expecting end of file (View: [...]\source_layoutsmaster.blade.php) (View: [...]
\source_layoutsmaster.blade.php)In 05187dc394c0a6d7c726c66dad2543a3097e1c8e.php line 22:
syntax error, unexpected 'endif' (T_ENDIF), expecting end of file (View: [...]\source_layoutsmaster.blade.php)
In 05187dc394c0a6d7c726c66dad2543a3097e1c8e.php line 22:
syntax error, unexpected 'endif' (T_ENDIF), expecting end of file
build [--pretty PRETTY] [-c|--cache [CACHE]] [--] [
]
It creates the following compiled code in the cache file:
<?php $__env->startComponent('Example', 'example', []); ?>
<?php $component->withAttributes([]); ?>
<?php if (isset($__componentOriginal0f01ed56a1e32a05e5ef96e4d779f34784af9a96)): ?>
<?php $component = $__componentOriginal0f01ed56a1e32a05e5ef96e4d779f34784af9a96; ?>
<?php unset($__componentOriginal0f01ed56a1e32a05e5ef96e4d779f34784af9a96); ?>
<?php endif; ?>
<?php echo $__env->renderComponent(); ?>
<?php endif; ?>
@royvanv sorry for closing this prematurely, you're right, that example doesn't work. Forcing the classname of the component you're registering to include a \ appears to fix this, so for now you can:
blade.php file (I don't think it matters what name you use, as long as there's a namespace Something; at the top of that file the build should work), or'\Icon' (not Icon::class)I'll try to dig deeper into this later this week.
Actually you can use a leading slash, I was just doing it wrong. This should work:
$bladeCompiler->component('\\Icon');
// or
$bladeCompiler->component('\\' . Icon::class);
I just want to start by saying thanks for this update to allow class based components! One quick question - is it possible to 'inject' the current $page instance to pass into a class-based component somehow? (the following code doesn't actually work, just a proof of concept for possible dependency injection?)
Something like:
<?php
namespace Components;
use Illuminate\View\Component;
use Illuminate\Container\Container;
use TightenCo\Jigsaw\PageVariable;
class Layout extends Component
{
public $page;
public function __construct(PageVariable $page)
{
$this->page = $page;
}
public function render()
{
return Container::getInstance()
->make('view')
->make('_components.layout');
}
}
This would allow components access to $page so we don't have to pass in :page="$page" every time:
// source/index.blade.php
<x-layout :page="$page">
<div class="p-8">
<h1 class="text-3xl font-bold">Hello world!</h1>
</div>
</x-layout>
Hey - coming across the same issue. Cannot access $page within a component.
Is there a workaround at all?
PS. Thanks for Jigsaw - I love it.
I don't think that's possible currently, no, since that page data isn't bound into the container. Based on some experimenting locally I don't think it should be too hard to add support for, we'll look into it!
That would be amazing!
Components seem the be the way Blade is going, and it鈥檚 a bit of a pain to have to pass it in each time.
Thanks!!!!!
Based on some experimenting locally I don't think it should be too hard to add support for, we'll look into it!
Thanks so much for looking into this @bakerkretzmar
Most helpful comment
I don't think that's possible currently, no, since that page data isn't bound into the container. Based on some experimenting locally I don't think it should be too hard to add support for, we'll look into it!