【问题标题】:Laravel/Blade - Extend same template multiple times on same pageLaravel/Blade - 在同一页面上多次扩展同一模板
【发布时间】:2014-11-19 11:58:13
【问题描述】:

所以必须有一个简单的方法来解决这个问题......在我的网站上,有多种模式,具体取决于页面。我创建了一个模态模板,这些都可以扩展。但是,我在页面上包含的最后一个模态最终会“接管”其余部分,因此我所有的模态最终都会与最后一个包含的部分相同。我怎样才能使每个扩展对于它扩展的文件都是唯一的?

正在发生的事情的示例:

//template.blade.php
<htmls and stuff>
    @yield('section_1')
    @yield('section_2')
</htmls and stuff>

//Modal 1
@extends('template')
@section('section_1')
    Some words
@stop
@section('section_2')
    More words
@stop

//Modal 2
@extends('template')
@section('section_1')
    Rabbit
@stop
@section('section_2')
    Stew
@stop

我最终没有加载两个独特的模态,而是加载了两个充满兔子炖肉的模态。

【问题讨论】:

    标签: laravel blade


    【解决方案1】:

    我个人会在这种情况下使用包含,除非您的部分中有标记。如果只是文本,您可以执行以下操作:

    //template.blade.php
    <htmls and stuff>
        {{ $section1 }}
        {{ $section2 }}
    </htmls and stuff>
    
    //Modal 1
    @include('template', ['section1' => 'Some words', 'section2' => 'More words'])
    
    //Modal 2
    @include('template', ['section1' => 'Rabbit', 'section2' => 'Stew'])
    

    【讨论】:

    • 嗯,是的,我已经学会了像这样传递字符串。然而,我实际上包括了标记 - 基本上每个模式都有不同的元素,有些涉及其他人没有的部分。希望有某种方法可以做到这一点,我正在使用引导模式,就标记而言,它们有点重。
    • 嗯我不确定,我相信这里有人会知道。你可能会做这样的事情@include('template', ['section1' =&gt; View::make('modals.section1', $data), 'section2' =&gt; View::make('modals.section2', $data)]) 但是它有点乱
    • @overwrite 代替 @endsection 会更好地解决问题
    【解决方案2】:

    我遇到了同样的问题。我也很想使用 Blade 模板,但最终使用了 php 包含,即使使用基本的 html 标记

    //Modal 1
    @include('layout.template', array(
    'section1'         => 
    '<h1>Modal 1</h1><p><b>Some</b> words</p>',
    
    'section2'         => 
    '<p>Some <u>words</u></p>'
    
    ))
    //Modal 2
    @include('layout.template', array(
    'section1'         => 
    '<h1>Modal 2</h1><p><b>Some</b> words</p>',
    
    'section2'         => 
    '<p>Some <u>words</u></p>
    <a href="http://stackoverflow.com"></a>'
    
    ))
    

    标记一切正常,包括链接。我遇到麻烦的地方是当我想使用包含 inside 包含数组时,我知道这是不可能的.这就是我想使用 Blade 模板的原因。

    【讨论】:

      【解决方案3】:

      尝试使用@overwrite 命令代替@endsection

      例子:

      @section('stuff')
           Stuff goes here...
      @overwrite
      

      来源:https://github.com/laravel/framework/issues/1058#issuecomment-17194530

      【讨论】:

      • 我认为第一次问这个问题时这个解决方案不存在,但这完美地解决了问题并且仍然允许复杂的标记。
      • 这是更好的答案,因为它可以解决比公认答案更广泛的问题。
      猜你喜欢
      • 1970-01-01
      • 2016-06-18
      • 2019-04-01
      • 1970-01-01
      • 2016-09-05
      • 1970-01-01
      • 1970-01-01
      • 2016-06-17
      • 1970-01-01
      相关资源
      最近更新 更多