如何从 laravel 4 中的包含视图修改布局部分


How to modify layout sections from included views in laravel 4?

所以,我基本上有一个组件,需要事先加载javascript。

主布局:

//layouts/master.blade.php
...
@yield('scripts')
...
@include('forms.search')
...

我的组件:

//forms/search.blade.php
@section('scripts')
some scripts here
@stop
...

我叫什么:

//main.blade.php
@extends('layouts.master')

这行不通。节不会添加到页眉中。我做错了什么还是根本不可能使用拉拉维尔?

您正在尝试在包含之前生成该部分。所以试试这个。

在你的//main.blade 中.php

@extends('layouts.master')
在//

layouts/master.blade 中.php

@include('forms.search')
和//

forms/search.blade.php

   some scripts here

你正在调用

@extends('layouts.master')

它有一个

@yield('scripts')

但是您正在声明该部分脚本forms/search.blade.php

因此,如果您检查正确,则是在错误的刀片模板上声明脚本,或者您将屈服区域放在错误的刀片模板上......因为由于@yield在layouts/master.blade.php上,因此它已经在@include之前执行,这不会扩展任何内容,因此声明@section无关紧要。

为了实现您想要的,

@section('scripts')
some scripts here
@stop 

部分应位于main.blade.php文件中。

如果我要这样做,它将是这样的:

layouts/master.blade.php

<html>
    <head>
        <!-- more stuff here -->
        @yield('scripts')
        <!-- or put it in the footer if you like -->
    </head>
    <body>
        @yield('search-form')
        @yield('content')
    </body>
</html>

forms/search.blade.php

//do whatever here

主刀片.php

@extends('layouts/master')
@section('scripts')
    {{ HTML::script('assets/js/search-form.js') }}
@stop
@section('search-form')
    @include('forms/search')
@stop

或者完全删除 master.blade 上的@yield('search-form').php 和 main.blade 上.php执行以下操作:

@section('scripts')
    {{ HTML::script('assets/js/search-form.js') }}
@stop
@section('content')
    @include('forms/search')
    <!-- other stuff here -->
@stop

我遇到了同样的问题,让它对我有用的是将"@parent"添加到我的所有部分......

{{-- Main area where you want to "yield" --}}
@section('js')
@show
@section('js')
  @parent
  {{-- Code Here --}}
@stop