zmyslny/laravel-inline-scripts
Composer 安装命令:
composer require zmyslny/laravel-inline-scripts
包简介
Converts your JavaScript code, stored in files, into a custom blade directive and inline it in HTML.
README 文档
README
A Laravel package that provides a simple way to wrap JavaScript code stored in a file into a PHP class and inline it using a custom Blade directive. The PHP wrapper class also allows you to modify the script before inlining (for example, by replacing placeholders).
Allows ✨:
- passing variables from PHP to JavaScript,
- process / modify the script in a dedicated PHP class.
Additionally - has build in ready-to-use scripts (built using this package):
- ColorSchemeSwitchThreeStates - light / dark - color scheme switching script (+ view with icons)
- ColorSchemeSwitchTwoStates - light / dark / system - color scheme switching script (+ view with icons)
- LivewireNavAdapter - color scheme navigation state adapter for Livewire
Requirements
- PHP 8.2 or newer
- Laravel 9.x or newer (wasn't tested with the older versions)
🚀 Quick Start
Install the package via Composer:
composer require zmyslny/laravel-inline-scripts
Register a custom Blade directive for your JavaScript in your AppServiceProvider:
class AppServiceProvider extends ServiceProvider { public function boot(): void { BladeInlineScripts::takeFiles( resource_path('js/your-first-script.js'), resource_path('js/your-second-script.js'), ... )->registerAs('myInlineScripts'); } }
Use the custom Blade directive in your template to inline the scripts:
<html> <head> ... @myInlineScripts </head> <body> ...
Done.
Using Built-in Scripts
The package includes ready-to-use scripts. For example, to add a color scheme switcher:
use Zmyslny\LaravelInlineScripts\Ready\ColorSchemeSwitchTwoStates\InitScript; use Zmyslny\LaravelInlineScripts\Ready\ColorSchemeSwitchTwoStates\SwitchScript; class AppServiceProvider extends ServiceProvider { public function boot(): void { BladeInlineScripts::take( new InitScript(), new SwitchScript('d') // toggle dark & light modes with 'd' key )->registerAs('colorScheme'); } }
Then use in your template:
@colorScheme
See the Color Scheme Switch, Color Scheme Three States, and LivewireNavAdapter documentation for full details and customization options.
Advanced Usage
For more advanced script processing, create a PHP class that implements the RenderableScript interface to prepare or transform your JavaScript code. Use the abstract class FromFile to load scripts from files, or FromFileWithPlaceholders to include placeholder replacement (e.g., __VARIABLE__ → value).
Register your custom scripts using BladeInlineScripts::take(...).
For a complete working example with detailed setup instructions, see the Color Scheme Switch documentation.
License
This package is licensed under the MIT License.
zmyslny/laravel-inline-scripts 适用场景与选型建议
zmyslny/laravel-inline-scripts 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 145 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 10 月 04 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 zmyslny/laravel-inline-scripts 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 zmyslny/laravel-inline-scripts 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 145
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 22
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-10-04