ryangjchandler/blade-capture-directive
Adds a @capture Blade directive to capture and store rendered template output in a variable for reuse later in the view. Useful for building snippets, components, and deferred sections without extra buffering or complicated view logic.
Installation:
composer require ryangjchandler/blade-capture-directive
No additional configuration is required—just use the directive in your Blade templates.
First Use Case:
@capture('header')
<h1>Welcome</h1>
<p>This is a reusable header.</p>
@endcapture
@header
This renders the captured content where @header is invoked.
Where to Look First:
@capture/@endcapture usage examples.resources/views directory for immediate testing.Reusable Components:
Use @capture to define modular sections (e.g., headers, footers, alerts) and reuse them across views without duplicating markup.
@capture('alert', ['type' => 'success'])
<div class="alert alert-{{ $type }}">{{ $slot }}</div>
@endcapture
@alert('User saved!')
Dynamic Content Injection:
Pass variables into captured blocks via @capture arguments:
@capture('user-card', $user)
<div class="card">
<img src="{{ $user->avatar }}">
<h3>{{ $user->name }}</h3>
</div>
@endcapture
@user-card($currentUser)
Conditional Rendering:
Combine with Blade’s @if for context-aware partials:
@capture('sidebar')
@if(auth()->check())
<nav>...</nav>
@endif
@endcapture
Layout Integration: Use in master layouts to avoid repetition:
@extends('layouts.app')
@section('content')
@capture('meta')
<meta name="description" content="{{ $pageDescription }}">
@endcapture
@meta
{{ $slot }}
@endsection
@capture for truly reusable snippets. Overuse can reduce readability.@capture('dashboard-header')).@component for complex logic:
@capture('modal-trigger')
<button @click="openModal">{{ $label }}</button>
@endcapture
<x-modal>
@modal-trigger('Delete')
<!-- Modal content -->
</x-modal>
Scope Leaks:
Variables defined inside @capture are not automatically available outside. Pass them explicitly:
@capture('stats', $metrics)
<p>Users: {{ $metrics['users'] }}</p>
@endcapture
Nested Directives:
Avoid nesting @capture inside other directives (e.g., @foreach). It can lead to unexpected output or errors.
Caching Quirks:
If using Blade caching (php artisan view:cache), recapture partials after changes:
php artisan view:clear
Dynamic Names Collisions:
Dynamically named captures (e.g., @capture($dynamicName)) may conflict. Prefer static names for clarity.
@capture and @endcapture are properly closed.@dump inside @capture to debug variables:
@capture('debug-data', $data)
@dump($data)
@endcapture
// config/view.php
'cache' => env('APP_DEBUG'),
Custom Directives:
Extend the package by creating your own directives (e.g., @slot-capture for named slots):
// app/Providers/BladeServiceProvider.php
Blade::directive('slot-capture', function ($name) {
return "<?php echo \$__env->startCapture(); ?>@capture({$name})";
});
Global Variables: Inject global variables into captures via a service provider:
Blade::composer('*', function ($view) {
$view->with('globalVar', 'value');
});
Testing: Mock captures in PHPUnit:
$this->blade->share('capturedContent', fn() => '<p>Mocked</p>');
$this->blade->render('@capturedContent');
How can I help you explore Laravel packages today?