【问题标题】:Laravel blade append to section not working properlyLaravel 刀片附加到部分无法正常工作
【发布时间】:2015-08-27 09:06:03
【问题描述】:

我想从视图写入模板中的一个区域,也从包含的视图写入第一个视图,但无论我尝试什么它都不起作用。我曾尝试使用@parent 和@append,但到目前为止还没有产生我想要的结果。

在我的布局中,我有以下内容:

@yield('scripts')

在我看来主要有以下几点:

@include('admin/rules/partials/_form')

@section('scripts)
    // javascript #1 here
@stop

在 admin/rules/partials/_form 我有以下内容:

@section('scripts)
    // javascript #2 here
@stop

就像我说的那样,我已经尝试在 @section('scripts') 之后使用 @parent 并且还使用 @append 而不是 @stop 但我所做的没有正确包含这两个脚本块。到目前为止,我所拥有的最好的是 javascript #1 被包含一次,而 javascript #2 被包含两次。我将如何做到这一点,以便每个代码块只附加到脚本区域一次?

【问题讨论】:

    标签: php laravel laravel-5 blade


    【解决方案1】:

    我最终解决了它,我必须执行以下操作:

    @yield('scripts')
    

    在我看来主要有以下几点:

    @include('admin/rules/partials/_form')
    
    @section('scripts)
        // javascript #1 here
    @stop
    

    在 admin/rules/partials/_form 我有以下内容:

    @section('scripts)
        @parent
        // javascript #2 here
    @overwrite
    

    【讨论】:

      【解决方案2】:

      This answer 为我工作。

      EDIT,但现在没有。

      编辑,我更新了下面的代码来工作。我在页面和布局上产生了javascript 部分。

      tests.layout.blade.php

      <!DOCTYPE html>
      <html lang="en">
          <head>
              <title>
                  @yield('title') - test
              </title>
              @yield('head')
          </head>
          <body>
      
          @yield('content')
      
          @yield('javascript')
          </body>
      </html>
      

      tests.page.blade.php

      @extends('layouts.default')
      @section('title', 'Poses')
      
      @section('content')
          <div class="container">
              <div class="row">
                  @include('tests.partials.one')
                  @include('tests.partials.two')
              </div>
          </div>
      @stop
      

      tests.partials.one.blade.php

      @section('content')
          @parent
          <h1>One</h1>
      @stop
      
      @section('javascript')
          @parent
          <script>Scripts from one</script>
      @stop
      

      tests.partials.two.blade.php

      @section('content')
          @parent
          <h1>Two</h1>
      @stop
      
      @section('javascript')
          @parent
          <script>Scripts from two</script>
      @stop
      

      更多参考:http://laravel.com/docs/5.0/templates

      【讨论】:

      • 我已经尝试过了,但是我得到了 javascript #1 包含一次和 javascript #2 包含两次
      • @geoffs3310 是的,它确实不起作用。它在 5.0 中工作我想知道它是否是 5.1 的“功能”?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-11-01
      • 2021-11-29
      • 2014-12-24
      • 2016-02-01
      • 1970-01-01
      • 2014-09-15
      • 2013-12-28
      相关资源
      最近更新 更多