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')">
我有一个 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>
所以当我浏览到一些 url 说 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.blade.php 而不是将其放在部分中。
例如在default.blade.php的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
根据我的说法,上述方法效果更好。
也许这可以帮到你。
我想实现 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')">
我有一个 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>
所以当我浏览到一些 url 说 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.blade.php 而不是将其放在部分中。
例如在default.blade.php的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
根据我的说法,上述方法效果更好。
也许这可以帮到你。