Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does @include work in laravel?

Tags:

php

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.

like image 618
kennysong Avatar asked Jul 26 '16 22:07

kennysong


People also ask

What is @include in Laravel?

@include is just like a basic PHP include, it includes a "partial" view into your view. @extends lets you "extend" a template, which defines its own sections etc. A template that you can extend will define its own sections using @yield , which you can then put your own stuff into in your view file.

How does Laravel blade work?

Laravel blade lets you extend views when you define a child page in the framework. It provides a clear directive that specifies which child page should you put up. Views that extend a blade layout tend to inject the content into the layout section using the different section directives.

What is yield (' content ') in Laravel?

In Laravel, @yield is principally used to define a section in a layout and is constantly used to get content from a child page unto a master page.

What are the two primary benefits of Laravel blade?

Two of the primary benefits of using Blade are template inheritance and sections. We can define a blade page as a combination of layout and sections. Since most of the general web applications will have the same layout across the web pages.


1 Answers

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); ?>";
    }
});
like image 177
Chris Avatar answered Oct 08 '22 15:10

Chris