How to @ enable work in laravel?

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

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

I want the user directive to return the html found in the path "authenticated / partials / header2", however, it seems that the blade server does not recognize that the path is the path in my php. My custom directive is in the AppServiceProvider.php file. Does anyone know how @include works very well, so they can explain why my path is not recognized.

+5
source share
2 answers

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

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

Thanks to Chris for your answer. That was right. However, if you want to pass certain variables to your view. Do below

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

Everything, including variables via compact (), threw me away.

0
source

All Articles