【问题标题】:Load different css/js for different views in laravel在 laravel 中为不同的视图加载不同的 css/js
【发布时间】:2018-08-29 22:44:11
【问题描述】:

我是 laravel 的新手。我使用 laravel 5.6 。我只想在一页(视图)中使用 Summernote 所见即所得编辑器。 Summernote 需要一个 css 和一个 js 文件。我只想在这个视图中加载这些文件。我该怎么做?
这是我尝试这样做的方法。
ma​​ster.blade.php 文件。

<html>

    @include('header')

    <body>

        @yield('content')

        @include('footer')

    </body>
</html>

editor.blade.php文件

@extends('master')

    @section('content')
        --- Summernote editor goes here---
    @endsection

    @section('imports')
    <link href="http://netdna.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.css" rel="stylesheet">
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.js"></script>
    <script src="https://netdna.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.js"></script>

    <!-- include summernote css/js-->
    <link href="http://cdnjs.cloudflare.com/ajax/libs/summernote/0.8.9/summernote.css" rel="stylesheet">
    <script src="https://cdnjs.cloudflare.com/ajax/libs/summernote/0.8.9/summernote.js"></script>

    <script>
        $(document).ready(function() {
            $('.summernote').summernote();
        });
    </script>
    @endsection

header.blade.php

<head>
    --- other css and file links ---

    @yield('imports')
</head>

你可以看到 editor.blade.php 文件扩展了 master.blade 文件。在主文件中,我包含了包含所有 css 链接的 header.blade 文件。所以我在 header.blade 文件中生成了 Summernote js/css。但是当它加载到浏览器时,header.blade 文件中生成的内容位于&lt;body&gt; 标记的开头(应该在&lt;head&gt; 标记的旁边)。
我可以直接将这些文件添加到 headr.blade.php 文件中。但我想知道是否有办法以这种方式做到这一点。

【问题讨论】:

  • 您能否发布实际代码:---- Summernote css and js file links here ---
  • 更新帖子@ChrisHappy

标签: php css laravel


【解决方案1】:

我通常以这种方式执行此操作,您不要在 master.blade 中使用 include() 刀片,因为无论如何它都会调用所有页面,只需 yield 然后在其他页面中注入到该 yield 部分。

master.blade.js

<html>

    @yield('header')

    <body>

        @yield('content')

        @yield('footer')

    </body>
</html>

header.blade.php

 @extends('master')

    @section('header')
            --- CSS here ---
    @endsection

    @section('content')
        --- content here ---
    @endsection

    @section('footer')
        --- scripts here ---
    @endsection

【讨论】:

  • 是的。这是一个非常好的方法。你有什么想法,为什么我的方法在这里不起作用。我们不能在包含的文件中使用 yield 吗?
  • 正如您在代码中看到的那样,您在“editor.blade”中扩展了“master.blade”,其中还包含您正在生成的代码(导入)这意味着所有内容都将转到生成的内容,在正文中。
  • 哦,对了。您的意思是,我们只能在一个文件中生成内容。对吗?
  • 你可以让出很多文件,但你不能包含你让出的扩展文件。
猜你喜欢
  • 2011-06-01
  • 2013-11-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-02-12
  • 2021-12-07
  • 1970-01-01
相关资源
最近更新 更多