Laravel Blade,全局元描述和子页面的不同元描述

问题描述 投票:0回答:3

我有一个index.blade,标头中包含所有必要的元标记,让我们以描述为例:

<meta name="description" content="This is the description">

这是我当前的索引文件

<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>@yield('site_title')</title>
    @yield('head_content')

    <meta name="description" content="This is the description">
    ...
</head>

现在,对于特定的子页面(/页面),我想要一个不同的描述,这就是我所拥有的:

@extends('layouts.app')
@section('head_content')

<meta name="description" content="This is a different description">

@endsection

不幸的是,这在最终的 html 子页面上添加了元描述。有没有办法为单个子页面提供不同的元描述,但同时具有全局描述?

php laravel laravel-blade meta-tags
3个回答
3
投票

您可以使用

@overwrite
代替
@endsection
... 覆盖原始内容:

@extends('layouts.app')
@section('head_content')
<meta name="description" content="This is a different description">
@overwrite

编辑:更改布局以使用部分而不是

@yield

<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>@yield('site_title')</title>
    @section('head_content')
    <meta name="description" content="This is the description">
    @show
    ...
</head>

第二次编辑: 如果您有多个元标记,您也可以在布局中使用类似的内容:

<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>@yield('site_title')</title>
    <meta name="description" content="@yield('meta_description', 'This is the description')">
    <meta name="keywords" content="@yield('meta_keywords', 'these,are,the,keywords')">
    ...
</head>

然后在您的子页面中您可以使用:

@extends('layouts.app')
@section('meta_description', 'A subpage description');
@section('meta_keywords', 'my,subpage,keywords');

1
投票

@overwrite
@endsection
相同,但替换现有内容

@extends('layouts.app')
@section('head_content')
<meta name="description" content="This is a different description">
@overwrite

文档


0
投票

这适用于 Laravel 10 吗?因为我使用 laravel 10,并且有多个页面,但它们都使用 main 中的元描述。

© www.soinside.com 2019 - 2024. All rights reserved.