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

时间:2013-09-17 18:00:58

标签: php laravel laravel-4 blade

所以, 我基本上有一个组件,需要事先加载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')

这不起作用。部分未添加到标题中。我做错了什么,或者根本不可能用laravel?

3 个答案:

答案 0 :(得分:1)

您试图在包含之前产生该部分。试试吧。

// main.blade.php

@extends('layouts.master')

// layouts / master.blade.php

@include('forms.search')

// forms / search.blade.php

   some scripts here

答案 1 :(得分:0)

你正在打电话

@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文件中..

如果我要这样做,那就是这样的:

<强>布局/ 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>

<强>形式/ search.blade.php

//do whatever here

<强> main.blade.php

@extends('layouts/master')

@section('scripts')
    {{ HTML::script('assets/js/search-form.js') }}
@stop

@section('search-form')
    @include('forms/search')
@stop

或者在 master.blade.php 上完全删除@yield('search-form'),在 main.blade.php 上删除{/ p>

@section('scripts')
    {{ HTML::script('assets/js/search-form.js') }}
@stop

@section('content')
    @include('forms/search')
    <!-- other stuff here -->
@stop

答案 2 :(得分:0)

我遇到了同样的问题,为我工作的是在我的所有部分添加“@parent”......

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

@section('js')
  @parent

  {{-- Code Here --}}
@stop