我正在使用laravel 4进行项目.有什么办法可以指定我想为某个视图加载的js文件.现在我把所有文件都集中在一起.
当我使用codeigniter,加载特定的js文件时,我使用库来生成脚本标记并在页脚处回显它们.如下的东西
$this->data['js'] = $this->js_lib->generate('jquery');
Run Code Online (Sandbox Code Playgroud)
然后在我看来
<?= $js ?>
Run Code Online (Sandbox Code Playgroud)
知道如何在laravel中做到这一点?
小智 21
主要布局
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>
Run Code Online (Sandbox Code Playgroud)
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>
Run Code Online (Sandbox Code Playgroud)
footer.blade.php
<footer>
<!-- common scripts -->
<script src="{{ asset('assets/js/jquery.min.js') }}"></script>
<!-- page specific scripts -->
@yield('pagespecificscripts')
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
小智 10
堆栈
Blade 允许您推送到命名堆栈,这些堆栈可以在另一个视图或布局中的其他地方呈现。这对于指定子视图所需的任何 JavaScript 库特别有用:
@push('scripts')
<script src="/example.js"></script>
@endpush
Run Code Online (Sandbox Code Playgroud)
您可以根据需要多次推送到堆栈。要呈现完整的堆栈内容,请将堆栈的名称传递给 @stack 指令:
<!-- Component Contents -->
@stack('scripts')
</body>
Run Code Online (Sandbox Code Playgroud)
https://laravel.com/docs/5.7/blade#stacks
我知道不久前问过,但为了其他可能在这里跌倒的人的利益!您的布局中还有一个选项可用于定义其他部分,例如
@yield('css') <!-- In the head -->
@yield('js') <!-- Before the </body> tag -->
Run Code Online (Sandbox Code Playgroud)
然后在你的意见
@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
Run Code Online (Sandbox Code Playgroud)
只需有一个“部分”视图文件夹 - 并在每个视图中包含您想要的任何脚本。
所以在你看来;
<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>
Run Code Online (Sandbox Code Playgroud)
然后有/views/partials/analytics.blade.php
<script>
// Your JS analytics script here
</script>
Run Code Online (Sandbox Code Playgroud)
然后对每个“脚本”重复