Open-Awesome
CategoriesAlternativesStacksSelf-HostedExplore
Open-Awesome

© 2026 Open-Awesome. Curated for the developer elite.

TermsPrivacyAboutGitHubRSS
  1. Home
  2. Laravel
  3. Laravel Blade Javascript

Laravel Blade Javascript

MITPHP2.9.0

A Laravel Blade directive to export PHP variables to JavaScript with a simple @javascript syntax.

Visit WebsiteGitHubGitHub
619 stars36 forks0 contributors

What is Laravel Blade Javascript?

Spatie Laravel Blade JavaScript is a Laravel package that adds a Blade directive to export PHP variables to JavaScript. It allows developers to seamlessly pass server-side data to client-side scripts using a simple `@javascript` syntax in Blade templates, eliminating the need for manual script tag creation and JSON encoding.

Target Audience

Laravel developers building applications that require passing server-side data to JavaScript, particularly those using Blade templates and wanting a clean, integrated solution.

Value Proposition

It provides a minimal, elegant syntax for a common task, reducing boilerplate code and potential errors. The package is lightweight, automatically registered, and configurable, making it a convenient alternative to manual implementations or heavier solutions.

Overview

A Blade directive to export variables to JavaScript

Use Cases

Best For

  • Passing Laravel session or config data to JavaScript
  • Injecting server-side variables into Vue or React components rendered with Blade
  • Simplifying data sharing between PHP and JavaScript in traditional server-rendered Laravel apps
  • Avoiding manual JSON encoding and script tag creation for small data transfers
  • Quickly prototyping features that require PHP data in client-side scripts
  • Namespacing JavaScript variables to avoid global scope pollution

Not Ideal For

  • Applications using modern JavaScript frameworks with client-side state management (e.g., Vuex, Redux) that handle data fetching independently
  • Projects requiring real-time data updates or streaming, as this package only injects data during Blade rendering
  • Environments with strict Content Security Policies that need automated nonce handling without manual view customization
  • Large-scale applications where passing complex or large datasets via script tags could impact performance or maintainability

Pros & Cons

Pros

Elegant Syntax

The @javascript directive simplifies variable passing with minimal code, eliminating the need for manual JSON encoding and script tag creation, as shown in the README examples.

Namespace Configuration

Allows variables to be scoped under a custom namespace to avoid global pollution, easily set via a config file, providing flexibility for organized codebases.

Customizable Output

Users can publish and edit the Blade view to modify the script tag, such as adding type attributes or CSP nonces, offering control over security and compliance.

Zero Configuration Setup

Automatically registers upon Composer installation, requiring no additional setup for basic usage, making it quick to integrate into Laravel projects.

Cons

Blade-Only Limitation

Confined to Blade templates, so it cannot be used in other Laravel contexts like APIs or console commands without workarounds, limiting its versatility.

Basic Data Serialization

Handles simple values well but may not efficiently manage complex nested objects or large datasets, as it outputs raw JavaScript assignments without optimization.

Security Overhead

Lacks built-in security features like automatic nonce generation; users must manually publish views to comply with strict CSP policies, adding maintenance burden.

Frequently Asked Questions

Quick Stats

Stars619
Forks36
Contributors0
Open Issues0
Last commit6 months ago
CreatedSince 2016

Tags

#blade#spatie#api#laravel#server-side-rendering#javascript#php#web-development#frontend-integration#view

Built With

B
Blade
P
PHP
L
Laravel

Links & Resources

Website

Included in

Laravel13.0k
Auto-fetched 10 hours ago

Related Projects

LiveWireLiveWire

A full-stack framework for Laravel that takes the pain out of building dynamic UIs.

Stars23,567
Forks1,745
Last commit14 hours ago
Debug BarDebug Bar

Debugbar for Laravel (Integrates PHP Debug Bar)

Stars19,277
Forks1,613
Last commit2 days ago
IDE HelperIDE Helper

IDE Helper for Laravel

Stars14,960
Forks1,194
Last commit18 days ago
Intervention ImageIntervention Image

PHP Image Processing

Stars14,369
Forks1,494
Last commit10 days ago
Community-curated · Updated weekly · 100% open source

Found a gem we're missing?

Open-Awesome is built by the community, for the community. Submit a project, suggest an awesome list, or help improve the catalog on GitHub.

Submit a projectStar on GitHub