拉拉维尔刀片模板 - 如何从扩展页面中删除零件



注意:就像我在评论中说的,我正在做一个相对较大的项目。我无法更改现有代码。只需尝试为一个页面添加一些代码块即可。

我有一个模板边栏选项卡。它具有元产量。但也包括一个meta.blade.php,其中包含所有元标记。但我不想为我的某些页面包含元页面。有可视化模板:

my_template

<header>
@yield('meta')
@include('metapage')
@yield('style')
@yield('js')
</header>

我的视图.刀片.php

@extends('my_template')
@section('meta')
<meta description...>
@endsection
@section('style')
//content
@endsection
@section('js')
//content
@endsection

我的问题是:有没有办法制作这样的东西:

@extends('my_template')->except('metapage')

我知道不存在这样的东西。但我需要那个。我希望有人能给我一个解决方案。

我有一个特定的解决方案:

  • 第一个是创建多个模板并根据您的要求扩展它们。
  • 第二个是禁用子部分。
  • 第三个创建具有小东西的父模板,然后创建扩展父模板并在此处执行额外操作的子模板。 根据需要使用它。

如果您正在处理现有项目并且有很多页面,那么第一个和第三个页面是一个更好的解决方案,因为您只能在前端进行更改而不会影响类代码。

你可以使元成为一个组件而不是一个包含,那么你的模板看起来是一样的,你的视图将是例如

@extends('my_template')
@section('meta')
@component('meta')
@slot('description', 'my amazing description')
@endcomponent
@endsection
// other code here as usual

然后,您的组件负责检查哪些存在,哪些不存在,例如:

@isset($description)
<meta name="description" content="{{ $description }}">
@endisset
@isset($title)
<title>{{ $title }}</title>
@endisset
// etc, the title is just an example

文档:https://laravel.com/docs/5.6/blade#components-and-slots

获取要忽略的标签名称并应用 CSS 例如:如果我需要从扩展页面中删除页脚,请制作

footer {
display: none;
}

最新更新