Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

load blade directives after resolving blade compiler #11

Merged
merged 1 commit into from
Feb 10, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 } ?>';
});
});