在laravel 5.3中添加css和js文件

时间:2016-09-30 09:40:23

标签: javascript php css laravel

我想在单页中包含一个css所有css和js并将其加载到所有页面中。现在如果我想在欢迎页面中包含bootstrap.css和bootstrap.js,我已经在welcome.blade.php页面中包含了如果我想添加另一个页面,我也包含在second.blade.php页面中。我想将它包含在母版页中

<link href="{{ asset('css/bootstrap.css') }}" rel="stylesheet">

4 个答案:

答案 0 :(得分:11)

Crate header.blade.php

var express = require('express');
var app = express();
app.listen(3000,fucntion(){
console.log("listening on server 3000")} 

并使用这样的 首先扩展标题 内容的第二部分

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="csrf-token" content="{{ csrf_token() }}" />
    <meta name="description" content="">
    <meta name="author" content="">

    <title>dhinchakwale</title>
    <!-- Bootstrap Core CSS -->
    <link href="{{ asset('/css/bootstrap.min.css') }}" rel="stylesheet">

    <link href="{{ asset('/css/datepicker.css') }}" rel="stylesheet" type="text/css">
    <link href="{{ asset('/css/bootstrap-timepicker.min.css') }}" rel="stylesheet" type="text/css">

    <link href="{{ asset('/css/dataTables.bootstrap.css') }}" rel="stylesheet">    
    <!-- Custom CSS -->  
    <link href="{{ asset('/css/admin.css') }}" rel="stylesheet">

    <!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
    <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
    <!--[if lt IE 9]>
        <script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
        <script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->

    <!-- jQuery -->
    <script src="{{ asset('/js/jquery.min.js') }}"></script>    
  </head>

  <body id="page-top">
    <nav class="navbar navbar-default">
      <div class="container-fluid">
        <!-- Brand and toggle get grouped for better mobile display -->        
        <div class="navbar-header">
          <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
            <span class="sr-only">Toggle navigation</span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
          </button>
          <a class="navbar-brand" href="/"><img alt="Brand" src="{{asset('/images/logo.png')}}" class="img-brand"></a>
        </div>      
      </div>
    </nav>
    <div class="content-div">
      @yield('content')
    </div>
    <div class="clearfix"></div>  

    <!-- Bootstrap Core JavaScript -->
    <script src="{{ asset('/js/bootstrap.min.js') }}"></script> 
    <script src="{{ asset('/js/bootstrap-datepicker.js') }}"></script>
    <script src="{{ asset('/js/bootstrap-timepicker.min.js') }}"></script>
  </body>
</html>

答案 1 :(得分:4)

以下是您可以遵循的步骤

  1. 创建主模板布局
  2. 在所有页面中扩展主模板,并将链接粘贴到标题部分
  3. 创建主模板 //master.blade.php

    <html lang="en">
    
          @yield('header')
    
    <body>
          @yield('page-body')
    </body>
    </html>
    

    现在在second.blade.php中扩展此布局

    @extend('master.blade')
    @section('header')
       <link href="{{ asset('css/bootstrap.css') }}" rel="stylesheet">
    @endsection
    @section('page-body')
       {{!-- content of body --}}
    @endsection
    

答案 2 :(得分:3)

定义一个公共布局,并在该布局上包含所有.js和.css文件,然后将页面与此布局绑定。

布局:

<html>
    <head>
        <title>App Name - @yield('title')</title>
    </head>
    <body>
        @section('sidebar')
            This is the master sidebar.
        @show

        <div class="container">
            @yield('content')
        </div>
    </body>
</html>

其中:@yield指令用于显示给定部分的内容。

查看:

@extends('layouts.default')
@section('content')
    i am the home page
@endsection

答案 3 :(得分:1)

您可以定义主布局视图文件,然后在那里定义CSS和JS。然后,您的所有视图文件都将扩展该布局文件。请参阅此处的文档:https://laravel.com/docs/5.3/blade