-
-
Notifications
You must be signed in to change notification settings - Fork 124
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
How to use blade components with this package? #57
Comments
I second this issue. From what I've read in the Laravel docs, you're supposed to be able to use but in doing so it throws the following error: Stack Trace: Any help with this issue would be greatly appreciated. |
Anonymous components ala 6.x work for me, fyi, like |
Is there any way this is fixed in the future? |
I was able to set up <?php
use Illuminate\Container\Container;
use Illuminate\Contracts\Foundation\Application as ApplicationContract;
use Illuminate\Contracts\View\Factory as ViewFactoryContract;
use Illuminate\Support\Facades\Facade;
use Illuminate\View\Component;
use Illuminate\View\View;
use Jenssegers\Blade\Blade as JenssegersBlade;
$app = Container::getInstance();
$app->bind(ApplicationContract::class, Container::class);
$app->alias('view', ViewFactoryContract::class);
$blade = new JenssegersBlade(
['views'],
'views/compiled',
$app
);
$viewFactory = Facade::getFacadeApplication()->get('view');
$blade->compiler()->components([
'media-image' => MediaImage::class,
'components.anonymous.breadcrumbs' => 'breadcrumbs' // yes, anonymous components with @props
]);
trait BladeViewComponent
{
public function view(string $name): View
{
global $viewFactory;
return $viewFactory->make("components.{$name}", $this->extractPublicProperties());
}
public function render(): View
{
return $this->view($this->template);
}
}
// Very rough example with some wordpress stuff
final class MediaImage extends Component
{
use BladeViewComponent;
public readonly WordPressMediaImage|bool $image;
private string $template = 'media-image';
/**
* @param string $wordpresstitle title of WP image attachment to process
* @param string $size size parameter for wp image functions
* @param string $class all the classes to assign to the img tag
* @param int $preload if the image needs to be preloaded
*/
public function __construct(
public readonly string $wordpresstitle,
public readonly string $size = 'full',
public readonly string $class = 'media-image',
public readonly int $preload = 0
) {
$this->image = $this->getWordPressMediaImage();
}
private function getAttachmentIdByTitle(): int|bool
{
$attachment = get_posts([
'numberposts' => 1,
's' => trim($this->wordpresstitle),
'lang' => pll_current_language('slug'),
'post_type' => 'attachment'
])[0];
if (!$attachment || !isset($attachment->ID)) {
error_log("Media image not found: {$this->wordpresstitle}", 1);
return false;
}
return $attachment->ID;
}
private function getWordPressMediaImage(): WordPressMediaImage|bool
{
if ($id = $this->getAttachmentIdByTitle()) {
return new WordPressMediaImage($id, $this->size);
}
return false;
}
} Then in your blade template you can put this: <x-media-image wordpresstitle="Background" class="home-bg-img" preload /> And @if ($image === false)
<h1>There has been an error loading image "{{ $wordpresstitle }}"</h1>
@else
@if ($preload)
@push('preload')
<link
rel="preload"
as="image"
href="{{ $image->src }}"
imagesrcset="{{ $image->srcsetWebp ? $image->srcsetWebp : $image->srcset }}"
imagesizes="{{ $image->sizes }}"
/>
@endpush
@endif
<picture>
@if ($image->srcsetWebp)
<source
srcset="{{ $image->srcsetWebp }}"
sizes="{{ $image->sizes }}"
type="image/webp"
/>
@endif
<source
srcset="{{ $image->srcset }}"
sizes="{{ $image->sizes }}"
type="{{ $image->metaData['sizes']['medium']['mime-type'] }}"
/>
<img
src="{{ $image->src }}"
width="{{ $image->metaData['width'] }}"
height="{{ $image->metaData['height'] }}"
alt="{{ $image->alt }}"
title="{{ $image->caption }}"
class="{{ $class }}"
loading="lazy"
/>
</picture>
@endif |
Ok, I forked this repo and made some updates to be able to use components (class-based and anonymous) |
@lexdubyna how did you set this up in a WordPress theme? Is it possible to render from a given HTML string (that contains x-components)? |
To be more specific, I'm calling the This is the complete code used in my WordPress plugin: <?php
/*
* Plugin Name: Spring reveal
*/
require plugin_dir_path( __FILE__ ) . '/vendor/autoload.php';
use Lexdubyna\Blade\Blade;
$blade = new Blade('views', 'cache');
add_action( 'init', function () {
ob_start();
} );
add_action( 'shutdown', function () {
global $blade;
$html = ob_get_clean();
echo $blade->compileString( $html );
exit;
}, 0 ); |
@kaspost You can check out how I implemented |
@lexdubyna Thank you and I'm using your forked version in composer. I boiled down the code to this bare minimum which still throws me the same error: <?php
/*
* Plugin Name: X Components test
*/
$plugin_dir = plugin_dir_path( __FILE__ );
require $plugin_dir . '/vendor/autoload.php';
use Lexdubyna\Blade\Blade;
$blade = new Blade( $plugin_dir . '/views', $plugin_dir . '/cache' );
echo $blade->compileString( '<x-test />' ); The folder content is: Is there something I'm missing here? |
@lexdubyna Where do we put our components? I am getting an error that App\View\Components\Alert not found |
@kaspost I have not yet tested x-components compiled from string. Will look into it. |
@Marvellous890 This is my test folders structure that works:
and code: $blade = new Lexdubyna\Blade\Blade(
[get_template_directory() . '/views'],
get_template_directory() . '/views/compiled'
);
$blade->compiler()->components([
'media-image' => App\Components\MediaImage::class,
'components.anonymous.alert' => 'alert'
]); |
No description provided.
The text was updated successfully, but these errors were encountered: