Stacks and Directives

Stacks, Fragments, and Custom Directives

A stack gathers content from any view: @push appends, @prepend puts first, @pushOnce adds once, and @stack('styles') prints. In Layouts with Components's <head>, the grid rule precedes the .featured rule that three cards pushed once.

A fragment serves htmx 79,053 or Turbo partial requests: the grid sits in @fragment('product-list'), and the route ends with ->fragmentIf($request->hasHeader('HX-Request'), 'product-list'):

Output of 61
$ curl -s -H 'HX-Request: true' localhost:8306/products | head -3
  <div class="cards">
          <article class="featured" data-id="1">
  <strong>The Pragmatic Programmer</strong><br><small>David Thomas, Andrew Hunt</small>

Custom directives go in AppServiceProvider::boot(). Blade::if('lowstock', fn (object $p) => $p->stock > 0 && $p->stock < 5) defined Conditionals's condition, with @else and @endlowstock for free. Blade::directive('money', fn (string $expr) => "<?php echo e(...); ?>") returns PHP source built from its argument text:

Output of 61
$ php artisan tinker --execute="echo Blade::compileString('@money(\$p->price_cents)');"
<?php echo e(\Illuminate\Support\Number::currency(($p->price_cents) / 100)); ?>
$ php artisan tinker --execute="echo Blade::render('@money(4995) / @money(129900)');"
$49.95 / $1,299.00

Its PHP is pasted into compiled views, so run view:clear after changing it and use full class names.