Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Blade Variable Laravel Package

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.

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Installation

    composer require arindam/blade-variable
    

    Publish the config (if needed):

    php artisan vendor:publish --provider="ArindamRoy\BladeVariable\BladeVariableServiceProvider"
    
  2. First Use Case Define a variable in a Blade template:

    @variable('user', ['name' => 'John Doe', 'email' => 'john@example.com'])
    

    Access it later in the same template:

    <p>Hello, {{ $user['name'] }}!</p>
    
  3. Where to Look First

    • Blade directives: Check @variable and @endvariable syntax.
    • Service Provider: BladeVariableServiceProvider registers the directive.
    • Config file: config/blade-variable.php (if published) for global defaults.

Implementation Patterns

Workflow: Scoped Variable Management

  1. Define and Use in a Single Template

    @variable('config', ['theme' => 'dark', 'layout' => 'sidebar'])
    <div class="{{ $config['theme'] }}">
        @include('partials.header', ['layout' => $config['layout']])
    </div>
    
  2. Pass Variables to Included Views

    @variable('sharedData', ['title' => 'Dashboard', 'items' => []])
    @include('layouts.app')
    

    Access in layouts/app.blade.php:

    <h1>{{ $sharedData['title'] }}</h1>
    
  3. Dynamic Variable Assignment Use PHP logic to conditionally define variables:

    @php
        $isAdmin = auth()->user()->isAdmin();
    @endphp
    @variable('permissions', $isAdmin ? ['create', 'edit', 'delete'] : ['view'])
    
  4. Integration with Blade Components Define variables in a component’s view:

    <x-user-card>
        @variable('user', $userData)
    </x-user-card>
    

    Access in the component’s template:

    <p>{{ $user['name'] }}</p>
    

Gotchas and Tips

Pitfalls

  1. Scope Limitation

    • Variables defined with @variable are template-scoped and not available in parent/child templates unless explicitly passed.
    • Fix: Use @include or @stack/@push for shared data.
  2. Overwriting Global Variables

    • If a variable name conflicts with Laravel’s global helpers (e.g., $errors), the Blade directive may not work as expected.
    • Fix: Use unique namespaced keys (e.g., ['app_data' => [...]]).
  3. No Direct Access in Controllers

    • Variables are Blade-only; they won’t be available in PHP logic outside the template.
    • Fix: Pass data via the controller’s view data array:
      return view('view.name', ['user' => $user]);
      
  4. Caching Quirks

    • If using Blade caching (php artisan view:cache), redefine variables in cached views may not persist.
    • Fix: Avoid @variable in cached views or clear cache after changes.

Tips

  1. Type Safety Use PHP’s json_encode to validate variable structure:

    @variable('user', json_decode('{"name":"John"}', true))
    
  2. Default Values Set defaults in the config file (config/blade-variable.php) for reusable variables:

    'defaults' => [
        'app' => ['name' => 'MyApp', 'version' => '1.0'],
    ],
    

    Access via @variable('app').

  3. Extending Functionality Override the directive logic by binding a new instance of BladeVariableManager in the service provider:

    $this->app->singleton('blade-variable', function () {
        return new \ArindamRoy\BladeVariable\BladeVariableManager(['custom' => 'value']);
    });
    
  4. Debugging Dump variables to check their contents:

    @variable('debug', ['data' => $someData])
    @dump($debug)
    
  5. Performance For large templates, define variables late (close to where they’re used) to avoid unnecessary scope pollution.

Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
emuniq/filament-browser-notifications
syriable/filament-translator
hungnm28/livewire-form
wenprise/eloquent
crudly/encrypted
fadion/bouncy
cuci/prototurk-sdk
gos/pubsub-router-bundle
cuci/prototurk-sdk-symfony
clementtalleu/easyadmin-markdown-bundle
codeflextech/permission-manager
karnoweb/livewire-datepicker
sayedenam/sayed-dashboard
milito/query-filter
apiboxsym/user-bundle
apiboxsym/health-check-bundle
jayeshmepani/jpl-moshier-ephemeris-php
elnasnato/laraliveui
labrodev/rest-sdk
sampaui/sampaui