zmyslny / laravel-inline-scripts
Converts your JavaScript code, stored in files, into a custom blade directive and inline it in HTML.
Package info
github.com/Zmyslny/laravel-inline-scripts
Type:package
pkg:composer/zmyslny/laravel-inline-scripts
Requires
- php: ^8.2
- laravel/framework: ^9.0|^10.0|^11.0|^12.0|^13.0
Requires (Dev)
- larastan/larastan: ^3.7
- laravel/pint: ^1.25
- laravel/sail: ^1.46
- orchestra/testbench: ^10.6
- pestphp/pest: ^3.8
- pestphp/pest-plugin-type-coverage: ^3.6
- rector/rector: ^2.1
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.