【问题标题】:How to modify layout sections from included views in laravel 4?如何从 laravel 4 中包含的视图修改布局部分?
【发布时间】:2013-09-22 07:11:14
【问题描述】:

所以, 我基本上有一个需要预先加载 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 根本不可能?

【问题讨论】:

  • 这肯定是可能的,我已经做过几次了。问题可能出在您的目录结构上。你的//layouts/master.blade.php 在哪里?是在views/layouts/master.blade.php 上吗?你的目录结构是什么样的?
  • 结构有什么问题?一切都在代码 cmets 中。所有文件都是由 laravel 加载的,不是目录问题

标签: php laravel laravel-4 blade


【解决方案1】:

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

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

@section('js')
  @parent

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

【讨论】:

    【解决方案2】:

    你在打电话

    @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
    

    ma​​in.blade.php

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

    或完全删除 ma​​ster.blade.phpma​​in.blade.php 上的 @yield('search-form') 这样做:

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

    【讨论】:

    • 你知道,问题是搜索表单和主模板不相关。主模板可以在没有此表单的情况下存在,并且表单可以在我需要的任何其他地方。所以据我所知,在主模板中没有办法做到这一点..
    • 第一个版本可以在没有表单的情况下使用。如果您想要的话,您还可以在控制器级别包含模板。您只需使用this 之类的容器准备将产生的内容,然后准备一个带有搜索表单的子布局和一个不带搜索表单的子布局。
    【解决方案3】:

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

    在你的 //main.blade.php

    @extends('layouts.master')
    

    而在 //layouts/master.blade.php

    @include('forms.search')
    

    还有//forms/search.blade.php

       some scripts here
    

    【讨论】:

      猜你喜欢
      • 2014-06-28
      • 2014-11-25
      • 2012-12-04
      • 2013-06-18
      • 1970-01-01
      • 2022-01-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多