The Dead Man
The Dead Man

Reputation: 5556

How to include custom script in laravel blade template?

I have a simple section in which I would like to use jquery, Here is what I have so far

@extends('layouts.app', ['activePage' => 'user-management', 'titlePage' => __('User Management')])

@section('content')
  <div class="content">
    <div class="container-fluid">
      <div class="row">
        <div class="col-md-12">

            <h1> TESTING inline JS </h1>
        </div>
      </div>
    </div>
  </div>
@endsection

@section('script')

<script type="text/javascript">

      alert('Imagination');

</script>
@endsection

When I run my app there is no alert just text Testing inline js

What is wrong with my code?

Upvotes: 0

Views: 5456

Answers (2)

cfkane
cfkane

Reputation: 733

The anwser above is correct, that you are missing the @yield('script') on your layouts.app. However, a better practice would be, instead of using a section, is to use @push('script') & @endpush then in your template to use @stack('script').

The advantage over using sections is that if you have multiple templates being rendered, the scrips for each of them will 'stack' where you use the stack in your main template.

You can see the docs on it here: https://laravel.com/docs/5.8/blade#stacks

Upvotes: 0

nakov
nakov

Reputation: 14248

So I assume that in your layouts/app.blade.php you are yielding the script section?

@yield('script')

Then you need to show the alert once the page loads.

So if you use jQuery as you say, add this code:

$(function() { // after the page has loaded..
    alert('Imagination');
});

Upvotes: 5

Related Questions