Closed PaulMorel closed 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.
@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.
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?
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
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 />
If you try to call a component within a Kirby snippet, an Exception gets thrown.
Take this setup for example:
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 thesite/snippets/header.blade.php
file tosite/templates/snippets/header.blade.php
and updatedefault.blade.php
accordingly.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)