Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Load specific js for certain pages in laravel

Tags:

laravel-4

I am using laravel 4 for a project. Is there any way I can specify what js files I want to load for a certain view. Right now I am lumping all in one file.

When I use codeigniter, to load specific js files, i use a library to generate the script tags and echo them at the footer. something as below

$this->data['js'] = $this->js_lib->generate('jquery');

Then in my view

<?= $js ?>

Any idea how to do this in laravel?

like image 875
kkh Avatar asked Mar 08 '14 03:03

kkh


5 Answers

Main Layout

main.blade.php

@include('includes.header')

<body>
    <!-- main content -->
    <div id="main_wrapper">
        <div class="page_content">
             @yield('content')
        </div>      
    </div>

@include('includes.footer')

</body>


header.blade.php

<head>
<meta charset="UTF-8">
<title>My Page</title>
<meta name="viewport" content="initial-scale=1.0,maximum-scale=1.0,user-scalable=no">

<!-- common styles -->
<link rel="stylesheet" href="{{ asset('assets/bootstrap.css') }}">      
<!-- page specific styles -->
    @yield('pagespecificstyles')
</head>

footer.blade.php

<footer>
<!-- common scripts -->
<script src="{{ asset('assets/js/jquery.min.js') }}"></script>

<!-- page specific scripts -->
@yield('pagespecificscripts')

mypage.blade.php

@extends('layouts.main')

@section('pagespecificstyles')

    <!-- flot charts css-->
    <link rel="stylesheet" href="{{ asset('assets/lib/owl-carousel/flot.css') }}">

@stop

@section('content')
<div class="container">
        Hello welcome to my page.
</div>
@endsection

@section('pagespecificscripts')
    <!-- flot charts scripts-->
    <script src="{{ asset('/assets/lib/flot/jquery.flot.min.js') }}"></script>
@stop
like image 77
RANJITH Avatar answered Sep 28 '22 23:09

RANJITH


Stacks

Blade allows you to push to named stacks which can be rendered somewhere else in another view or layout. This can be particularly useful for specifying any JavaScript libraries required by your child views:

@push('scripts')
    <script src="/example.js"></script>
@endpush

You may push to a stack as many times as needed. To render the complete stack contents, pass the name of the stack to the @stack directive:

    <!-- Component Contents -->
    @stack('scripts')
</body>

https://laravel.com/docs/5.7/blade#stacks

like image 21
pady Avatar answered Sep 28 '22 23:09

pady


I know asked a while ago but for the benefit of others who may stumble here! One option is also in your layout to define additional sections, e.g.

@yield('css') <!-- In the head -->

@yield('js') <!-- Before the </body> tag -->

Then in your views

@extends('some.layout')

@section('css')
    @include('js/dependencies/some/js/dependency/css.css')
@append

<!-- So in case these dependencies are used elsewhere you're not repeating your script or link tags over and over -->

@section('js')
    @include('js/dependencies/some/js/dependency/js.js') 
    @include('js/dependencies/some/js/dependency/js2.js')
@append

@section('content')

    Your actual content

@endsection
like image 37
André Figueira Avatar answered Sep 28 '22 23:09

André Figueira


Just have a 'partials' view folder - and include whatever script you want in each view.

So in your view;

<body>
    // Your main content here
    // Other JS files you want in all views

    @include('partials.analytics.blade.php')

    @include('partials.googlemaps')

    @include('partials.some_other_js_file')

</body>

Then have /views/partials/analytics.blade.php

<script>
  // Your JS analytics script here
</script>

and just repeat for each 'script'

like image 37
Laurence Avatar answered Sep 28 '22 23:09

Laurence


You may add the following code in your app.blade file

@stack('scripts')

and then you can use the jquery on page

@push('scripts')
<script type="text/javascript">
 ///Js code
</script>
@endpush
like image 36
Kaushik shrimali Avatar answered Sep 29 '22 01:09

Kaushik shrimali