laravel blade include files with relative path
Asked Answered
C

4

13

In laravel blade system when we want to include a partial blade file we have to write the full path every time for each file. and when we rename a folder then we will have to check every @include of files inside it. sometimes it would be really easy to include with relative paths. is there any way to do that?

for example we have a blade file in this path :

resources/views/desktop/modules/home/home.blade.php

and I need to include a blade file that is near that file :

@include('desktop.modules.home.slide')

with relative path it would be something like this :

@include('.slide')

is there any way to do this?

Commanding answered 18/4, 2018 at 7:58 Comment(3)
Like: @include('./././slide')??Veinstone
but a "relative" path would be with ../Blunger
I read a few minutes ago blade doesn't allow relative path. I think it always get from route, because I always need to put full path. A better choice is to create the detail components using VUE, despite to use @include.Blunger
P
8

if someone still interest with relative path to current view file, put this code in the boot method of AppServiceProvider.php or any provider you wish

    Blade::directive('relativeInclude', function ($args) {
        $args = Blade::stripParentheses($args);

        $viewBasePath = Blade::getPath();
        foreach ($this->app['config']['view.paths'] as $path) {
            if (substr($viewBasePath,0,strlen($path)) === $path) {
                $viewBasePath = substr($viewBasePath,strlen($path));
                break;
            }
        }

        $viewBasePath = dirname(trim($viewBasePath,'\/'));
        $args = substr_replace($args, $viewBasePath.'.', 1, 0);
        return "<?php echo \$__env->make({$args}, \Illuminate\Support\Arr::except(get_defined_vars(), ['__data', '__path']))->render(); ?>";
    });

and then use

    @relativeInclude('partials.content', $data) 

to include the content.blade.php from the sibling directory called partials

good luck for everyone

Photojournalism answered 1/2, 2020 at 20:42 Comment(1)
Good solution! Works for me on Laravel 8.Pennell
M
4

There’s a (now unmaintained) package doing both relative and absolute includes (lfukumori/laravel-blade-include-relative) working with @include, @includeIf, @includeWhen, @each and @includeFirst directives. I just pulled it in a project, it works well.

Muniz answered 13/7, 2019 at 9:21 Comment(0)
P
3

A sleek option, in case you want to organise view files in sub-folders:

public function ...(Request $request) {
    $blade_path = "folder.subfolder.subsubfolder.";
    $data = (object)array(
        ".." => "..",
        ".." => $..,
        "blade_path" => $blade_path,
    );
    return view($data->blade_path . 'view_file_name', compact('data'));
}

Then in the view blade (or wherever else you want to include):

@include($blade_path . 'another_view_file_name')
Pea answered 26/8, 2020 at 4:46 Comment(0)
G
2

you need to create custom blade directive for that, the native include directive doesn't work like that.

read this page to learn how to create custom blade directive :

https://scotch.io/tutorials/all-about-writing-custom-blade-directives

\Blade::directive('include2', function ($path_relative) {
    $view_file_root = ''; // you need to find this path with help of php functions, try some of them.
    $full_path = $view_file_root . path_relative;
    return view::make($full_path)->render();
});

then in blade file you can use relative path to include view files :

@include2('.slide')

I tried to tell you the idea. try and test yourself.

Guillerminaguillermo answered 15/8, 2018 at 10:16 Comment(1)
Please include the essence of the solution in your answer, as it otherwise might become useless if the linked website changes or goes offline.Afrit

© 2022 - 2024 — McMap. All rights reserved.