arindam/blade-variable
Declare and use variables directly in Laravel Blade templates with a simple @var directive. Install via Composer (supports auto-discovery; provider available if needed) and set values like @var('name','Arindam') to access as $name in views.
arindam/blade-variable) enables defining variables directly in Blade templates, which is useful for:
livewire or inertia where template variables are critical.{!! !!} vs {}).View Composer or Service Providers achieve the same goal with better separation?blade-ui-kit, livewire). No conflicts expected.$title = "Home") with Blade-defined ones:
@variable('title', "Home")
@directive usage.php artisan view:clear). Variables may need to be marked as cacheable explicitly.@variable('user', $user) <!-- May not update on Livewire property changes -->
composer require arindam/blade-variable).config/app.php to register the service provider (if required; check package docs).@variable and test rendering.{!! !!} for untrusted data).@variable('user.role', auth()->user()->role)).## Blade Variable Guidelines
- Use for **UI-only** data (e.g., static strings, computed CSS classes).
- Avoid for **business logic** (e.g., user permissions, API calls).
- Always escape dynamic content: `@variable('name', e($userInput))`.
@variable overuse.dd() or dump(). Use:
@dump(@variable('debug_var'))
@variable('expensive_data', DB::table('users')->get()) <!-- Anti-pattern -->
@variable('data', Cache::remember('key', 60, fn() => getExpensiveData()))
| Failure Scenario | Impact | Mitigation |
|---|---|---|
| Unsanitized variable XSS | User data injection in HTML/JS | Enforce e() escaping; use {!! !!} sparingly. |
| Variable overwrites global scope | Breaks other Blade directives | Prefix variables (e.g., @variable('ui.header')). |
| Template caching issues | Stale variables after updates | Use ?random cache busting or view()->flush(). |
| Frontend framework misalignment | Inertia/Livewire state conflicts | Test variable reactivity in staging. |
| Package abandonment | No updates for Laravel 11+ | Fork or replace with custom Blade directives. |
@variable vs. controllers.{!! !!}).@variable('is_admin', auth()->user()->is_admin) <!-- Boolean -->
@variable('menu_items', $navItems->take(5)) <!-- Collection -->
@variable usage.@variable beyond pilot phase.@variable improve your workflow?"How can I help you explore Laravel packages today?