【问题标题】:Blade - (Sage 9) probem when multiple @yield/@sectionBlade - 多个@yield/@section 时的(Sage 9)探测
【发布时间】:2019-09-26 12:43:02
【问题描述】:

我们是 sage9 / Blade 框架的新手,我们正在尝试使用 ACF 创建模板逻辑。 1 块时一切正常,但当我们添加更多块时,前 1 块会回显 2-3 次。

我们是这样做的:

我们使用来自 sage9 的默认 layouts.app:

web/app/themes/[theme-name]/resources/views/layouts/app.blade.php

...
    <div class="vy_main uk-offcanvas-content">
      <header>
        @yield('dispatch_banner') 
      </header>
      <div class="wrap container" role="document">
        <div class="content">
          <main class="main">
            @yield('dispatch') //The one we currently working on
          </main>
...

在布局中我们调用@yield('dispatch')。在 page.blade.php 中,我们扩展了布局并添加了 dispatch 部分。

web/app/themes/[theme-name]/resources/views/page.blade.php

@extends('layouts.app')

@section('dispatch_banner')
  @layouts('banners')
      {!! App::banners() !!} 
  @endlayouts
@endsection


@section('dispatch')
  @layouts('sections')
      {!! App::sections() !!} //This call a controller where we can select the correct section to display.
  @endlayouts
@endsection

在控制器内部, web/app/themes/[theme-name]/app/Controllers/App.php 我们返回要使用的模板并传递要使用的配置/变量。 :

public static function sections(){
 ...
   $return .= \App\template('sections.'.$sections, $config);
  }

  return $return;
 ...
}

我们创建一个标准块。此块由调度程序包含:

web/app/themes/[theme-name]/resources/views/sections/std.blade.php

在这个模板中,我们创建了一个新的布局“base”,因为所有的部分都将具有相同的基础结构,我们在模板中扩展这个基础并在其中放置一个部分内容,如下所示:

web/app/themes/[theme-name]/resources/views/sections/std.blade.php

@extends('layouts.base')

@section('section-content')
  @if ($image)
    <div class="uk-grid uk-flex-middle" data-uk-grid>
      <div class="{!! $class_image !!}">
          <img src="{!! $image['url'] !!}" alt="{!! $image['alt'] !!}">
      </div>
      <div class="{!! $class_content !!}">
  @endif 
      <div class="{!! $content_class_content !!}">
          @layouts('content')
            {!! App::content() !!}
          @endlayouts
      </div>

  @if ($image)  
      </div>
    </div>
  @endif 
@endsection

这里的布局 web/app/themes/[theme-name]/resources/views/layouts/base.blade.php

<section {{ (( $section_id )?'id='.$section_id:'') }} class="{!! $class_section !!}">

  @if($has_container)
    <div class="uk-container uk-container-{!! $container !!}" data-uk-scrollspy="cls: uk-animation-fade;"> 
      @yield('section-content')
    </div>
  @else 
    @yield('section-content')
  @endif

</section>

正如我所说,使用 1 个块一切正常,但只要我们添加第二个块,数据就会继续重复只有来自基础的 @yield('section-content'),变量在布局不会重复。

这是我们在 html 中的内容:

&lt;section {{ (( $section_id )?'id='.$section_id:'') }} class="{!! $class_section !!}"&gt;

我们得到:

<section class="uk-section vy_std uk-section-primary uk-section-xlarge">
<section class="uk-section vy_accordion uk-section-transparant">
<section class="uk-section vy_std uk-section-transparant">

我们的逻辑哪里出了问题,@yield('section-content') 的内容不断重复,而不是使用控制器发送的正确数据?

如果这可以帮助我从控制器发送所有代码,它并不大,但对我来说这不是问题所在,所以我把这部分删掉了。

感谢您的宝贵时间!

【问题讨论】:

  • 只是补充一下,如果我们删除布局“base”并将代码放在节标准中,它会按预期工作,所以看起来确实是多个“@section('section-content')”成为这里的问题......

标签: php wordpress laravel-blade


【解决方案1】:

我设法通过使用组件/插槽而不是布局来解决这个问题。现在我从这样的组件中获取基础:

@component('layouts.base', $config)
  /*Section code*/
@endcomponent

一切都恢复正常了!

【讨论】:

    猜你喜欢
    • 2017-05-16
    • 2021-03-29
    • 2019-04-18
    • 1970-01-01
    • 2018-06-10
    • 2018-04-25
    • 2017-11-18
    • 1970-01-01
    • 2016-05-19
    相关资源
    最近更新 更多