doulongsi1831 2015-06-25 09:21
浏览 51
已采纳

如何告诉Laravel要包含哪些脚本文件

How can I tell Laravel-5 which Javascript files I want to include in a template.

For example, I have my Controller with something like this:

return view('butchery', ['locations' => $locations, 'classTypes' => $classTypes]);

I have my butchery.blade.php template with my html:

@extends('app')
@section('content')
<!-- html here -->
@endsection

...and I have my app.blade.php container template with all my javascript files at the bottom. If I didn't want to include one of the javascript files in my view, how would I do that?

app.blade.php:

<!DOCTYPE HTML>
<!--[if IE 8]><html class="ie8"> <![endif]-->
<!--[if gt IE 8]><!--><html><!--<![endif]-->

<head></head>
<body>@yield('content')</body>
<script src="/js/mobile-menu.js"></script>
<!-- apply voucher code --> 
<script src="/js/voucher-apply.js"></script>
</html>
  • 写回答

2条回答 默认 最新

  • doutan8775 2015-06-25 09:37
    关注

    You should create a section in your template where the scripts should be loaded and use this in your pages to include the required scripts.

    Like so:

    Template:

    <html>
        <head>
            <title>My page</title>
        </head>
        <body>
            <div class="container">
                @yield('content')
            </div>
            @yield('scripts')
        </body>
    </html>
    

    Page:

    @extends("template")
    
    @section('content')
        <h1>Hello world!</h1>
    @stop
    
    @section('scripts')
        <script src="/path/to/script.js"></script>
    @stop
    

    This way you can include the scripts on the page that are needed. Scripts that are needed through the entire template can be added under or above the @yield('scripts').

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?