Skip to content

Commit

Permalink
allow basset blocks to skip the cache
Browse files Browse the repository at this point in the history
  • Loading branch information
pxpm committed Jul 16, 2024
1 parent dc420ca commit 49c11b5
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
15 changes: 14 additions & 1 deletion src/BassetManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -231,10 +231,23 @@ public function basset(string $asset, bool|string $output = true, array $attribu
* @param string $code
* @return StatusEnum
*/
public function bassetBlock(string $asset, string $code, bool $output = true): StatusEnum
public function bassetBlock(string $asset, string $code, bool $output = true, bool $cache = true): StatusEnum
{
$this->loader->start();

// when cache is set to false we will just mark the asset as loaded to avoid
// loading the same asset twice and return the raw code to the browser.
if($cache === false) {
if($this->isloaded($asset)) {
return $this->loader->finish(StatusEnum::LOADED);
}
$this->markAsLoaded($asset);

echo $code;

return $this->loader->finish(StatusEnum::LOADED);
}

// Get asset path and url
$path = $this->getPathHashed($asset, $code);

Expand Down
9 changes: 7 additions & 2 deletions src/BassetServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,16 @@ protected function registerBladeDirectives()

// Basset Code Block
$bladeCompiler->directive('bassetBlock', function (string $parameter): string {
return "<?php \$bassetBlock = {$parameter}; ob_start(); ?>";
$parameters = array_map('trim', explode(',', $parameter));
$blockName = $parameters[0];
// keep cache as true by default when not provided in block definition
$cacheOption = $parameters[1] ?? true;

return "<?php \$bassetBlock = {$blockName}; \$cache = {$cacheOption}; ob_start(); ?>";
});

$bladeCompiler->directive('endBassetBlock', function (): string {
return '<?php Basset::bassetBlock($bassetBlock, ob_get_clean()); ?>';
return '<?php Basset::bassetBlock($bassetBlock, ob_get_clean(), true, $cache); ?>';
});

// DEPRECATED - Please use `@basset` or `@bassetBlock`. @loadOnce Will be completely removed in Backpack v7.
Expand Down

0 comments on commit 49c11b5

Please sign in to comment.