degecko/laravel-blade-inline 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

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

  1. At compile time, @inline reads the partial's Blade source
  2. Strips any @props directive (unnecessary since variables come from the parent scope)
  3. Compiles the Blade to PHP via Blade::compileString()
  4. 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 @props is stripped, the $attributes bag is not available. Use explicit variables instead.
  • Cache: Changes to the partial require php artisan view:clear to 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 $attributes or component features

License

MIT

统计信息

  • 总下载量: 1
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 5
  • 点击次数: 8
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 5
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-04-13

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固