Laravel - 使用@section动态设置元标记

Dar*_*Lau 2 php laravel laravel-blade

我想实现Facebook分享按钮功能,它将在用户点击分享按钮期间捕获元标签信息.

我有一个head.blade.php,其中包含一些要捕获的元标记.例

<meta property="og:url" content="@yield('facebook_share_url')">
<meta property="og:image" content="@yield('facebook_share_image')">
<meta property="og:description" content="@yield('facebook_share_description')">
Run Code Online (Sandbox Code Playgroud)

我有一个default.blade.php.

<!doctype html>
<html>
<head>
    @include('includes.head')
</head>
<body>
<div class="container">

    <header class="row">
        @include('includes.header')
    </header>

    <div id="main" class="row">
        @yield('content')
    </div>
</div>

@include('includes.footer')
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

因此,当我浏览一些网址www.example.com/details时,default.blade.php将从此details.blade中获取内容.此时我想更改facebook元标记属性,所以我在详细信息页面中执行了类似下面的操作.

@section('facebook_share_image', 'This is a description')
@section('facebook_share_description', 'This is an individual page title')
Run Code Online (Sandbox Code Playgroud)

代码工作正常,但我想从我的视图数据中呈现它.就像是@section('facebook_share_description', {{ $data->description }})

但是当我检查我的元标记时,它什么也没给我.有可能这样做吗?

Sai*_*nce 6

我不确定这是你想要的还是你想要的,但为什么不尝试只将meta标签放在default.blade.php而不是放在局部.

例如,在headdefault.blade.php的标记中插入:

@yield('facebook_meta')
Run Code Online (Sandbox Code Playgroud)

view_file.blade.php

@section('facebook_meta')
    <meta property="og:url" content="Place your data here">
    <meta property="og:image" content="Place your data here">
    <meta property="og:description" content="Place your data here">
@endsection
Run Code Online (Sandbox Code Playgroud)

据我所知,上述方法效果更好.

也许这可以帮到你.