【问题标题】:Laravel @section not showingLaravel @section 没有显示
【发布时间】:2017-05-01 06:48:25
【问题描述】:

我的问题是,当我在 laravel 刀片(不是 master)中定义一个部分时,我无法在页面上显示它的内容,但是如果我在 master.blade.php 上生成它,它可以正常工作。那么如何让这个页面显示出来:

“内容”部分 “模板”部分

    @extends('layouts.master')

@section('title', 'Website Builder')
@section('content')
    <script type="text/javascript" src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
    <script type="text/javascript" src="{!! asset('js/template.js') !!}"></script>
@endsection
        @section('template') 
        <div class= "container template_class ">
            @foreach ($templates as $template)
        <a class="content-link" href="{{ asset($template->file )}}">
        <img id = "image" src="{{ asset($template->image )}}"/>
        </a>
            @endforeach
        </div>
 <div id="content-link2"></div>
            <div class="container">
            @yield('content')
            @yield('template')
        </div>
@endsection
@show

【问题讨论】:

  • 文档不够清楚吗?
  • 我不会问是否清楚
  • 伙伴请重新阅读文档,您所要求的内容就在那里……“阅读”我的意思是阅读不仅仅是略读。 In this example, the sidebar section is utilizing the @parent directive to append (rather than overwriting) content to the layout's sidebar. The @parent directive will be replaced by the content of the layout when the view is rendered.

标签: laravel laravel-5 laravel-blade


【解决方案1】:

因此,如果您已经在主布局中定义了 @section,它将被覆盖,除非您在子布局的 @section 中指定 @parent

但是对于@yield,它总是从子布局中获取部分。这意味着它总是覆盖@yield 部分,即使它的默认值定义为@yield('section', 'Default Content')

希望这行得通!

【讨论】:

  • 好的,为什么我的@section('template') 没有显示,记住它是一个子布局
  • @PrzemekWojtas 你有你的@yield('template') 在你里面@section('template')@endsection。您需要将您的 @yield('template') 放在您的 @section('template') @endsection 之外。
  • 这是有道理的,但是为什么@yield('template') 被应用于整个网站的所有页面,而我只在这个子布局中使用它?
  • @RobinDirksen,tutorials.kode-blog.com/laravel-blade-template 的 Laravel 博士省略了 @parent,并表示主布局的文本将显示在 output 中。只需转到“扩展主布局”部分,您就会找到它。有什么解释吗?
猜你喜欢
  • 2017-05-16
  • 2018-04-25
  • 2019-01-15
  • 2023-03-16
  • 1970-01-01
  • 2014-09-08
  • 2016-05-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多