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


Laravel - set meta tag dynamically with @section

我想实现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')">

我有一个默认的.blade.hp.

<!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>

因此,当我浏览到一些网址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')

代码运行良好,但我想从我的视图数据中呈现它。类似@section('facebook_share_description', {{ $data->description }})

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

我不确定这是您想要的还是您想要的,但为什么不尝试将meta标记仅放在default.flade.php中,而不是放在分部中呢。

例如,在default.blade.hp:的head标签中插入

@yield('facebook_meta')

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

根据我的说法,上述方法效果要好得多

也许这能帮你。