-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11 from DigitallyHappy/load-blade-directives-afte…
…r-resolving-blade-compiler load blade directives after resolving blade compiler
- Loading branch information
Showing
1 changed file
with
28 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,40 @@ | ||
<?php | ||
|
||
use Illuminate\Support\Str; | ||
use Illuminate\View\Compilers\BladeCompiler; | ||
|
||
Blade::directive('loadStyleOnce', function ($parameter) { | ||
return "<?php Assets::echoCss({$parameter}); ?>"; | ||
}); | ||
$this->app->afterResolving('blade.compiler', function (BladeCompiler $bladeCompiler) { | ||
$bladeCompiler->directive('loadStyleOnce', function ($parameter) { | ||
return "<?php Assets::echoCss({$parameter}); ?>"; | ||
}); | ||
|
||
Blade::directive('loadScriptOnce', function ($parameter) { | ||
return "<?php Assets::echoJs({$parameter}); ?>"; | ||
}); | ||
$bladeCompiler->directive('loadScriptOnce', function ($parameter) { | ||
return "<?php Assets::echoJs({$parameter}); ?>"; | ||
}); | ||
|
||
Blade::directive('loadOnce', function ($parameter) { | ||
// determine if it's a CSS or JS file | ||
$cleanParameter = Str::of($parameter)->trim("'")->trim('"')->trim('`'); | ||
$filePath = Str::of($cleanParameter)->before('?')->before('#'); | ||
$bladeCompiler->directive('loadOnce', function ($parameter) { | ||
// determine if it's a CSS or JS file | ||
$cleanParameter = Str::of($parameter)->trim("'")->trim('"')->trim('`'); | ||
$filePath = Str::of($cleanParameter)->before('?')->before('#'); | ||
|
||
// mey be useful to get the second parameter | ||
// if (Str::contains($parameter, ',')) { | ||
// $secondParameter = Str::of($parameter)->after(',')->trim(' '); | ||
// } | ||
// mey be useful to get the second parameter | ||
// if (Str::contains($parameter, ',')) { | ||
// $secondParameter = Str::of($parameter)->after(',')->trim(' '); | ||
// } | ||
|
||
if (substr($filePath, -3) == '.js') { | ||
return "<?php Assets::echoJs({$parameter}); ?>"; | ||
} | ||
if (substr($filePath, -3) == '.js') { | ||
return "<?php Assets::echoJs({$parameter}); ?>"; | ||
} | ||
|
||
if (substr($filePath, -4) == '.css') { | ||
return "<?php Assets::echoCss({$parameter}); ?>"; | ||
} | ||
if (substr($filePath, -4) == '.css') { | ||
return "<?php Assets::echoCss({$parameter}); ?>"; | ||
} | ||
|
||
// it's a block start | ||
return "<?php if(! Assets::isLoaded('".$cleanParameter."')) { Assets::markAsLoaded('".$cleanParameter."'); ?>"; | ||
}); | ||
// it's a block start | ||
return "<?php if(! Assets::isLoaded('".$cleanParameter."')) { Assets::markAsLoaded('".$cleanParameter."'); ?>"; | ||
}); | ||
|
||
Blade::directive('endLoadOnce', function () { | ||
return '<?php } ?>'; | ||
$bladeCompiler->directive('endLoadOnce', function () { | ||
return '<?php } ?>'; | ||
}); | ||
}); |