beebmx / kirby-blade

Enable Blade for Kirby 3 & 4
MIT License
15 stars 3 forks source link

Blade anonymous components don't work within snippets #3

Closed PaulMorel closed 4 years ago

PaulMorel commented 4 years ago

If you try to call a component within a Kirby snippet, an Exception gets thrown.

Take this setup for example:

{{-- site/templates/components/alert.blade.php --}}
<div class="alert">
    Hello
</div>
{{-- site/snippets/header.blade.php --}}
<header>
    <x-alert/>
</header>
{{-- site/templates/default.blade.php --}}
<!doctype html>
<html>
    <head>
    </head>
    <body>
        @snippet('header')
    </body>
</html>

An exception gets thrown: Method Illuminate\View\View::__toString() must not throw an exception, caught InvalidArgumentException: Unable to locate a class or view for component [alert].


I thought maybe I could use Blade's @include directive instead as a workaround, so I moved the site/snippets/header.blade.php file to site/templates/snippets/header.blade.php and update default.blade.php accordingly.

{{-- site/templates/default.blade.php --}}
<!doctype html>
<html>
    <head>
    </head>
    <body>
        @include('snippets.header')
    </body>
</html>

Doesn't work either. I get a different Exception: Method Illuminate\View\View::__toString() must not throw an exception, caught ErrorException: Call to a member function get() on array (View: D:\Dev\www\example\staging\site\templates\snippets\header.blade.php) (View: D:\Dev\www\example.com\staging\site\templates\snippets\header.blade.php) (View: D:\Dev\www\example.com\staging\site\templates\snippets\header.blade.php)

beebmx commented 4 years ago

Right now the components only works outside snippets (only in the templates directory). But if you need to implement the same behavior.

You can do something like:

{{-- site/templates/components/alert.blade.php --}}
<div class="alert">
    Hello
</div>
{{-- site/templates/partials/header.blade.php --}}
<header>
    <x-alert/>
</header>
{{-- site/templates/default.blade.php --}}
<!doctype html>
<html>
    <head>
    </head>
    <body>
        @include('partials.header')
    </body>
</html>

And you will get the same result.

PaulMorel commented 4 years ago

@beebmx I appreciate the fast reply. I already tried that work around and I get another Exception. I updated my post at the same time you replied.

beebmx commented 4 years ago

I used exactly you blade files and I can't reproduce your error. And the result is the expected. Is there anything else in the code?

image
PaulMorel commented 4 years ago

Yes there's other code, I just didn't include it because I didn't think it was relevant. Turns out it was. I found the problem.

I had another @snippet inside my template that seems to conflict with includes and components. If I change @snippet for @include it works. I don't think it's 100% related with the original issue though. I'll investigate further in the future.

Thanks for the help

beebmx commented 4 years ago

There's no problem. I think the new version with components and snippets are great. The code at the end looks cleaner.

And if you want to do it even cleaner, you can use a component for your header.

<x-header />