degecko / laravel-blade-inline
Inline Blade partials at compile time for faster rendering in loops
Requires
- php: ^8.2
- illuminate/support: ^11.0|^12.0|^13.0
Requires (Dev)
- orchestra/testbench: ^11.1
- phpunit/phpunit: ^13.1
README
Inline Blade partials at compile time for faster rendering in loops.
The problem
When you use @include inside a loop, Laravel's view factory resolves the file, creates a new view instance, and compiles the template on every iteration. For a page rendering 100+ cards, this overhead adds up.
{{-- Each iteration pays the full view factory cost --}} @foreach ($items as $item) @include('components.card') @endforeach
The solution
@inline compiles the partial once and embeds the resulting PHP directly into the parent view at compile time. At runtime, the loop body is just plain PHP — no view factory, no file lookups, no ComponentAttributeBag.
{{-- Compiled once, inlined as raw PHP --}} @foreach ($items as $item) @inline('components.card') @endforeach
Benchmarks
Rendering an escort listing card across different loop sizes (Laravel 13, PHP 8.4):
| Cards | @include |
@inline |
Improvement |
|---|---|---|---|
| 13 | 1.61ms | 1.51ms | 6% |
| 104 | 13.6ms | 10.1ms | 26% |
| 260 | 32.6ms | 27.6ms | 16% |
The improvement grows with template complexity and loop size.
Installation
composer require degecko/laravel-blade-inline
The service provider is auto-discovered.
Usage
Basic usage
@inline works like @include but the partial shares the parent's variable scope:
@foreach ($products as $product) @inline('components.product-card') @endforeach
The $product variable is available inside the partial automatically — no need to pass it explicitly.
Passing variables
You can set local variables for the partial:
@foreach ($products as $product) @inline('components.product-card', [ 'highlight' => $loop->first, 'lazy' => $loop->index > 6, ]) @endforeach
How it works
- At compile time,
@inlinereads the partial's Blade source - Strips any
@propsdirective (unnecessary since variables come from the parent scope) - Compiles the Blade to PHP via
Blade::compileString() - Embeds the compiled PHP directly into the parent view
The result: the partial's logic becomes part of the parent view's compiled PHP file. At runtime, there's zero overhead from view resolution.
Important notes
- Shared scope: The partial accesses the same variables as the parent view. No isolated scope like
@include. - No
$attributes: Since@propsis stripped, the$attributesbag is not available. Use explicit variables instead. - Cache: Changes to the partial require
php artisan view:clearto take effect (same as any Blade change in production). - All Blade directives work:
@if,@foreach,@php/@endphp,{{ }}, etc. are all fully supported.
When to use
Use @inline when:
- A partial is rendered many times in a loop (listing pages, tables, feeds)
- The partial doesn't need isolated variable scope
- You want to eliminate view factory overhead
Stick with @include when:
- The partial is rendered once or twice (no loop overhead to save)
- You need isolated variable scope
- You use
$attributesor component features
License
MIT