Skip to content

Commit

Permalink
Merge pull request #11 from DigitallyHappy/load-blade-directives-afte…
Browse files Browse the repository at this point in the history
…r-resolving-blade-compiler

load blade directives after resolving blade compiler
  • Loading branch information
tabacitu authored Feb 10, 2022
2 parents 98d9eb8 + 103c100 commit a323b8f
Showing 1 changed file with 28 additions and 25 deletions.
53 changes: 28 additions & 25 deletions src/blade_directives.php
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 } ?>';
});
});

0 comments on commit a323b8f

Please sign in to comment.