Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message
Blade Capture Directive

Blade Capture Directive Laravel Package

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.

Deep Wiki
Context7

ryangjchandler/blade-capture-directive adds a simple @capture directive to Laravel Blade so you can capture rendered output into a variable instead of echoing it immediately. It’s ideal for building reusable view fragments, composing complex layouts, and passing pre-rendered HTML into components or slots.

Use it when you need cleaner templates and want to avoid awkward buffering or manual string concatenation.

  • Capture blocks of Blade output into a named variable
  • Keeps templates tidy by separating generation from rendering
  • Works seamlessly with existing Blade components, slots, and layouts
  • Great for conditional markup and composing sections programmatically
Frequently asked questions about Blade Capture Directive
How do I install ryangjchandler/blade-capture-directive in my Laravel project?
Run `composer require ryangjchandler/blade-capture-directive` in your project root. No additional configuration is needed—it integrates automatically with Laravel’s Blade engine. Ensure your project uses PHP 8.1 or higher for compatibility.
What’s the difference between @capture and Laravel’s @include directive?
@capture stores rendered output in a variable for later use in the same view, while @include inserts content directly. Use @capture for reusable fragments within a template (e.g., dynamic modals) and @include for standalone partials like headers or footers.
Can I use @capture with Laravel Livewire or Alpine.js?
Yes, but test reactivity carefully. Captured content won’t automatically update Livewire/Alpine components—you’ll need to manually re-render or use Alpine’s `x-data` to sync dynamic changes. Avoid over-nesting @capture blocks in reactive contexts.
Does this package work with Laravel 10+ and Blade components?
Absolutely. The package is designed for modern Laravel (10+) and plays well with Blade components, slots, and layouts. You can capture output inside components or pass it to slots like `@slot('sidebar') {{ $capturedContent }} @endslot`.
How do I debug issues if @capture isn’t working as expected?
Check for syntax errors in the captured block (e.g., unclosed tags). Use `{{ dump($capturedVariable) }}` to inspect the stored output. If issues persist, verify no other Blade directives are conflicting, and ensure your template isn’t cached (run `php artisan view:clear`).
Will @capture improve performance compared to manual string concatenation?
Yes, it eliminates the need for `ob_start()`/`ob_get_clean()` hacks, which can slow down rendering. However, overusing @capture in deeply nested templates may slightly increase compilation time. Benchmark against your current approach for critical sections.
Can I use @capture to build dynamic UI snippets for dashboards?
Perfectly suited for dashboards! Capture reusable elements like cards, modals, or alerts into variables, then conditionally render them based on user roles or data. Example: `@capture('user-card') {{ $user->renderCard() }} @endcapture` and reuse `$userCard` elsewhere.
Are there alternatives to @capture for Laravel Blade?
Yes: Use `@include` for static partials, view composers for logic-heavy sections, or JavaScript frameworks (e.g., Alpine/Vue) for client-side reactivity. However, @capture is unique for *inline* variable storage without file extraction—ideal for legacy apps or tight templates.
How does @capture handle errors in captured content?
Errors in captured blocks (e.g., undefined variables) will throw Blade exceptions like any other template syntax. Wrap dynamic content in `@if` checks or use `{{ $var ?? '' }}` to prevent silent failures. Test edge cases like malformed HTML or missing data.
Should I migrate existing @include directives to @capture for better reusability?
Only if the partial is used *multiple times* in the same view. For standalone includes (e.g., layouts), stick with `@include` for clarity. Use @capture sparingly for inline snippets—overuse can make templates harder to debug. Start with non-critical sections first.
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
davejamesmiller/laravel-breadcrumbs
artisanry/parsedown
christhompsontldr/phpsdk
enqueue/dsn
bunny/bunny
enqueue/test
enqueue/null
enqueue/amqp-tools
milesj/emojibase
bower-asset/punycode
bower-asset/inputmask
bower-asset/jquery
bower-asset/yii2-pjax
laravel/nova
spatie/laravel-mailcoach
spatie/laravel-superseeder
laravel/liferaft
nst/json-test-suite
danielmiessler/sec-lists
jackalope/jackalope-transport