degecko/laravel-blade-inline
最新稳定版本:1.2.0
Composer 安装命令:
composer require degecko/laravel-blade-inline
包简介
Inline Blade partials at compile time for faster rendering in loops
README 文档
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
统计信息
- 总下载量: 1
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 5
- 点击次数: 8
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2026-04-13