Amin Adel
Amin Adel

Reputation: 1028

laravel blade include files with relative path

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?

Upvotes: 14

Views: 14444

Answers (4)

meduz'
meduz'

Reputation: 618

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.

Upvotes: 4

Chandras Batheja
Chandras Batheja

Reputation: 29

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')

Upvotes: 2

Mohammad Al-Ani
Mohammad Al-Ani

Reputation: 576

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

Upvotes: 9

Emin Gorgani
Emin Gorgani

Reputation: 88

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.

Upvotes: 1

Related Questions