This package contains a Blade directive to export values to JavaScript.
Here's an example of how it can be used:
@javascript('key', 'value')
The rendered view will output:
<script>window['key'] = 'value';</script>
So in your browser you now have access to a key variable:
console.log(key); //outputs "value"
Spatie is a webdesign agency based in Antwerp, Belgium. You'll find an overview of all our open source projects on our website.
You can install the package via composer:
composer require spatie/laravel-blade-javascript
The package will automatically register itself.
Optionally the config file can be published with
php artisan vendor:publish --provider="Spatie\BladeJavaScript\BladeJavaScriptServiceProvider"
This is the contents of the published config file:
return [
/**
* All passed values will be present in this JavaScript namespace. Set this to an empty string
* to use the window object.
*/
'namespace' => '',
];
With the package installed you can make use of a @javascript
Blade directive.
@javascript('key', 'value')
The rendered view will output:
<script>key = 'value';</script>
You can also use a single argument:
@javascript(['key' => 'value'])
This will also output:
<script>key = 'value';</script>
When setting the namespace to eg js
in the config file this will be the output:
<script>window['js'] = window['js'] || {};js['key'] = 'value';</script>
Please see CHANGELOG for more information what has changed recently.
$ composer test
Please see CONTRIBUTING for details.
If you discover any security related issues, please email [email protected] instead of using the issue tracker.
You're free to use this package, but if it makes it to your production environment we highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using.
Our address is: Spatie, Samberstraat 69D, 2060 Antwerp, Belgium.
We publish all received postcards on our company website.
This repository contains some code from the laracasts/PHP-Vars-To-Js-Transformer package written by Jeffrey Way.
Spatie is a webdesign agency based in Antwerp, Belgium. You'll find an overview of all our open source projects on our website.
Does your business depend on our contributions? Reach out and support us on Patreon. All pledges will be dedicated to allocating workforce on maintenance and new awesome stuff.
The MIT License (MIT). Please see License File for more information.