Laravel 4:如何注册一个带有局部视图的脚本,以便它在javascript依赖之后加载?

ral*_*oss 2 javascript uiview blade laravel-4

有一个Laravel 4网站,它使用在页面底部加载jquery,boostrap等的布局:

<html>
   <body>

      <!-- stuff -->

      <script src="...jquery, bootstrap, etc..."></script>
   </body>
</html>
Run Code Online (Sandbox Code Playgroud)

在网站的某些页面中,我也使用了部分视图,视图使用的脚本依赖于页面底部的脚本.为了保持清洁,我希望脚本成为视图文件本身的一部分,这样无论何时包含视图,它都包含相应的javascript.但是,当脚本是视图的一部分时,它会失败,因为脚本标记将位于页面底部的依赖项之前.

有没有什么好的方法可以使用像刀片@yield@section指令这样的东西来保持脚本与视图在同一个文件中,但是将它加载到javascript依赖项下面的布局部分?我不相信我可以使用@yield/ @section因为部分没有@extend任何布局.只是@included在需要它的视图中.

所以我最终想要的是:

partial.blade.php:

<div><!-- partial's html stuff --></div>
<script><!-- partial's script stuff --></div>
Run Code Online (Sandbox Code Playgroud)

view.blade.php:

@extends('layout')

@section('content')
   @include('partial')
@stop
Run Code Online (Sandbox Code Playgroud)

layout.blade.php

<html>
   <body>
      @yield('content')

      <script><!-- jquery, bootstrap, etc. --></script>

   </body>
</html>
Run Code Online (Sandbox Code Playgroud)

最终的html输出:

<html>
   <body>

      <div><!-- partial's html stuff --></div>

      <script src="...jquery, bootstrap, etc..."></script>

      <script><!-- partial's script stuff --></div>
   </body>
</html>
Run Code Online (Sandbox Code Playgroud)

显然,简单的答案是在<head>标记中加载依赖项,但我想知道是否可以避免这种情况.

tli*_*kos 8

你可以试试这个.我测试了它,我相信它有效.

布局

<html>

   <body>

        @yield('content')   

        <script>'Scripts From Layout'</script>

        @yield('scripts')

   </body>

</html>
Run Code Online (Sandbox Code Playgroud)

视图

@extends('layout')

@section('content')

   @include('partial')

@stop

@section('scripts')

   @include('partial')

@stop
Run Code Online (Sandbox Code Playgroud)

局部

@section('content')

    <div>DIV From Partial</div>

@stop

@section('scripts')

    <script>'Scripts From Partial'</script>

@stop
Run Code Online (Sandbox Code Playgroud)

OUTPUT

<html>

   <body>

    <div>DIV from partial</div>

    <script>'Scripts From Layout'</script>

    <script>'Script From Partial'</script>

   </body>

</html>
Run Code Online (Sandbox Code Playgroud)