2015-06-11 53 views
4

我想從我的模板從視圖,也從一個包含的視圖寫東西的第一個視圖,但無論我嘗試它不起作用。我曾嘗試使用@parent和@append,但沒有產生我想要的結果。Laravel刀片追加到部分不能正常工作

在我的佈局我有以下幾點:

@yield('scripts') 

在我看來,主要我有以下幾點:

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

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

在管理/規則/諧音/ _form我有以下幾點:

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

所以就像我說過的,我在@section('scripts')之後試過使用@parent,並且也使用了@append @stop,但我沒有做任何事情,包括正確的兩個腳本塊。到目前爲止,我所擁有的最好的功能是包含一次javascript#1,並且包含兩次javascript#2。我將如何做到這一點,以便每個代碼塊只附加到腳本區域一次?

回答

4

我解決它在最後,我不得不做以下幾點:

@yield('scripts') 

在我看來,主要我有以下幾點:

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

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

在管理/ rules/partials/_form我有以下內容:

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

This answer爲我工作。

編輯,但現在它不。

編輯,我更新了下面的代碼來工作。我正在產生頁面上的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 

測試。 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

+0

我試過,但我得到的JavaScript#1包括一次和JavaScript#2包括兩次 – geoffs3310

+0

@ geoffs3310是的,它肯定不起作用。它工作在5.0我不知道它是5.1的一個「特徵」嗎? – whoacowboy