kennysong
kennysong

Reputation: 524

How does @include work in laravel?

I am trying to write a custom directive in laravel. However, it only returns the path of my blade partial as a string, not the actual html like @include does.

@customInclude('authenticated/partials/header2') 


    Blade::directive('customInclude', function($partial){
        if(Config::get('constants.ORG_ID') === 'organizationId'){
            return "<?php echo $partial; ?>";
        }
    });

I want the custom directive to return the html found in the path 'authenticated/partials/header2' , however, it seems that blade is not recognizing that the path is a path in my php. My custom directive lives in the AppServiceProvider.php file btw. Does anyone know how @include works really well so they can explain why my path isn't being recognized.

Upvotes: 6

Views: 1055

Answers (2)

kennysong
kennysong

Reputation: 524

Thanks Chris for your answer. It was correct. However, if you want to pass in specific variables to your view. Do the below

    Blade::directive('customInclude', function($partial){
        if(Config::get('constants.ORG_ID') === 'organizationId'){
            return "<?php echo view($partial,compact('variable1','variable2','variable3')); ?>";
        }
    });

The whole including variables through compact() threw me off.

Upvotes: 0

Chris
Chris

Reputation: 58182

Cool question, it took a bit of digging, but you can replicate what laravel does quite easily:

Blade::directive('customInclude', function($partial){
    if(Config::get('constants.ORG_ID') === 'organizationId'){
        return "<?php echo view($partial); ?>";
    }
});

Upvotes: 2

Related Questions