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 handy @capture Blade directive to grab the rendered output of a section of a view into a variable, so you can reuse it later in the template. Useful for building dynamic attributes, slots, and content fragments without messy output buffering.

View on GitHub
Deep Wiki
Context7

Create inline partials in your Blade templates with ease.

Frequently asked questions about Blade Capture Directive
How do I install the Blade Capture Directive package in my Laravel project?
Run `composer require ryangjchandler/blade-capture-directive` in your project root. No additional configuration is needed—it integrates seamlessly with Laravel’s Blade engine. Ensure your project uses PHP 8.1+ for compatibility.
Can I use @capture to replace @include directives for reusable components?
Yes, but consider the trade-offs. @capture is ideal for inline snippets (e.g., dynamic cards, modals) where you want to avoid separate view files. For larger components, @include may still be better for maintainability. Test both in your workflow.
Will this package work with Laravel Livewire or Alpine.js?
Yes, but verify reactivity. @capture works with Livewire/Alpine, but nested or dynamically rendered content may require explicit binding (e.g., Alpine’s `x-data`). Test edge cases like conditional rendering inside captured blocks.
Does the @capture directive support nested blocks or conditional logic?
Absolutely. You can nest @capture blocks or combine them with Blade conditionals (e.g., `@capture('header') @if(auth()->check()) ... @endif @endcapture`). However, deep nesting may reduce template readability—use sparingly for complex UIs.
How does this affect template caching in Laravel?
Inline partials via @capture may slightly reduce caching efficiency if overused, as they’re rendered dynamically. For static content, prefer @include or view composers. Benchmark your app’s template compilation time post-adoption.
Are there any risks of breaking existing Blade directives or custom syntax?
Minimal risk, but audit your existing directives first. @capture is a standalone directive and shouldn’t conflict with Laravel’s built-in ones (e.g., @stack, @once). If you use custom directives, test them alongside @capture.
Can I use @capture with Laravel Mix or Vite for asset management?
Yes, but manage dependencies carefully. If your captured partials include JS/CSS, ensure assets are properly enqueued (e.g., via `@stack` or Mix/Vite). Avoid duplicating assets across multiple @capture blocks.
What’s the best way to test templates using @capture in Laravel?
Test with PHPUnit’s `render()` method or snapshot testing (e.g., Pest’s `assertViewIs()`). Focus on edge cases like malformed Blade syntax inside captured blocks or dynamic content. Mock dependencies where needed.
Should I use @capture for dynamic content like user-specific widgets?
Yes, but weigh the pros and cons. @capture excels at dynamic inline snippets (e.g., personalized dashboards). For highly dynamic content, pair it with Livewire or Alpine for reactivity. Avoid overusing it for logic-heavy sections.
What are the alternatives to @capture for inline Blade partials?
Alternatives include Laravel’s built-in `@include` directives, view composers, or JavaScript-based components (e.g., Vue/React). @capture is unique for its inline, variable-based approach—ideal for Blade-heavy apps where you want to avoid extra files.
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.
hamzi/corewatch
minionfactory/raw-hydrator
hexters/coinpayment
rjcodes/rjcms
act-training/laravel-permissions-manager
alimarchal/laravel-chart-of-accounts
babenkoivan/elastic-scout-driver
mkwebdesign/filament-watchdog-v5
renatomarinho/laravel-page-speed
zedmagdy/filament-business-hours
renatovdemoura/blade-elements-ui
devgeek/beacon-admin
benjamin-rqt/data-watcher-bundle
atriumphp/atrium
sandermuller/package-boost-laravel
sandermuller/boost-skills
redaxo/core
yusufgenc/filament-api-forge
l3aro/rating-star-for-filament
leek/filament-subtenant-scope