1

I need to include a js file in my views.

But in this js file i need to interpret somes PHP variable.

Actually i do this :

@section('javascript')
    <script>
        alert("{{{test}}}");
    </script>
@stop

But i REALLY need to do this :

@section('javascript')
    {!! Html::script('js/test.js') !!}
@stop

test.js :

alert("{{{test}}}");

I need to declare a lot o variable. So my JS file will be very huge. And i don't want to show this directly in the source code.

How can i do ? Thank you !

CanardMandarin
  • 303
  • 3
  • 11

3 Answers3

0

You can only pass the variable to the javascript like so:

@section('javascript')
<script>
    var test = {{{$test}}};
</script>
@stop

then in your javascript file included at the bottom you can use it:

alert(test);

Let me just mention that this is not a great way of handling the passing variables from php to javascript.

Pawel Bieszczad
  • 12,925
  • 3
  • 37
  • 40
  • Yeah why not ! But i need to declare a lot of variable x). It will be dirty to show this in the source code. I prefer to include a js file with all the declaration. – CanardMandarin Oct 07 '15 at 18:43
  • The variables will show in the page source code unless you get them with an ajax call after the page loads. If you want to clean it up in the blades, create a partial, that accepts `$data` array, loops over it and creates all the javascript variables, and include that partial to needed views. – Pawel Bieszczad Oct 07 '15 at 18:47
0

When I need to do something like this, I usually create a meta tag on the page which would contain the alert information.

<meta name="someAlertValue" content="{{{ $test }}}" />

Then you can very easily grab that via jQuery.

var alert_text = $('meta[name=someAlertValue]').attr('content');

I find this approach to be much cleaner and maintainable than trying to drop php variables directly into your javascript.

user1669496
  • 32,176
  • 9
  • 73
  • 65
  • I need to declare a lot of variable x). So the head tag will be very huge. I prefer to declare the variable in JS file. But thank you ! – CanardMandarin Oct 07 '15 at 18:44
  • You can just as easily store JSON in in the content attribute and it would contain all your variables in one shot. http://stackoverflow.com/questions/7322682/best-way-to-store-json-in-an-html-attribute – user1669496 Oct 07 '15 at 19:03
0

I had the same problem, and wanted to have a stand alone js which will have bunch of variables taken from config() or even from database, multi-language, and will be configurable, or even will work with query parameters. Maybe it's a hard way, but i've created a route:

Route::get('/js-modules/test.js',function(){ return view('js-modules.test');})->name('my_js);

So in the view resources/views/js-modules/test.blade.php you can put your js code together with your PHP stuff.

Or you can route it to a controller and have even more background work. it looks a bit slow (in the browser) on the first run , but second request it'll be cashed and retrieved as the regular js file

And now you can link it to any of your pages with

<script src="{{route('my_js')}}"></script>

Shirker
  • 1,233
  • 2
  • 18
  • 30