/laravel-blade-include-relative

Allows to include blade template with relative path based on current view.

Primary LanguagePHPMIT LicenseMIT

Laravel Blade Include Relative

Allows to include blade template with relative path based on current view.

Installation

Require this package with composer.

composer require fukumori/laravel-blade-include-relative

Clear view cache BEFORE usage

php artisan view:clear

Usage

Make your view including sub-view with relative path

<!-- Stored in resources/views/site/some-file.blade.php -->

{{-- full include with hint --}}
@include('site::partials.included-view', ['name' => 'site::partials.included-view'])
{{-- full include (normal usage) --}}
@include('site.partials.included-view', ['name' => 'site.partials.included-view'])
{{-- relative include --}}
@include('partials.included-view', ['name' => 'partials.included-view'])
{{-- relative includeIf --}}
@includeIf('partials.included-view', ['name' => 'if partials.included-view'])
{{-- relative includeWhen --}}
@includeWhen(true, 'partials.included-view', ['name' => 'when partials.included-view'])
{{-- relative each --}}
@each('partials.included-view', ['each1 partials.included-view', 'each2 partials.included-view'], 'name')

Make your sub-view

<!-- Stored in resources/views/site/partials/included-view.blade.php -->

<div>Included view with: {{ $name ?? '' }}.</div>

Call your view

<!-- Stored in routes/web.php -->

Route::view('/test', 'site.some-file');

See the magic appear

<div>Included view with: site::partials.included-view.</div>
<div>Included view with: site.partials.included-view.</div>
<div>Included view with: partials.included-view.</div>
<div>Included view with: if partials.included-view.</div>
<div>Included view with: when partials.included-view.</div>
<div>Included view with: each1 partials.included-view.</div>
<div>Included view with: each2 partials.included-view.</div>

Know Issues

If a view was previously loaded with a name and does not exist in the current paths, the last valid view with that name will be include.