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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34368156/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 12:50:11  来源:igfitidea点击:

Laravel - set meta tag dynamically with @section

phplaravellaravel-blade

提问by Darren Lau

i would like implement facebook share button function whereby it will capture the meta tags information during user hit the share button.

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

I've a head.blade.php that include some meta tag to be captured. Example

我有一个 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')">

And i have a default.blade.php.

我有一个 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>

So when i browse to some url says www.example.com/details, the default.blade.php will yield the content from this details.blade. And at this moment i would like to change the facebook meta tag properties, so i did something like below in details page.

所以当我浏览到某个 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')

The code work fine but i would like to render it from my view data. Something like @section('facebook_share_description', {{ $data->description }})

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

but when i check my meta tag, it gives me nothing. Is it possible to do so?

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

回答by Saiyan Prince

I am not sure whether this is what you want or what you are looking for, but why not try placing the metatags in the default.blade.phponly instead of placing it in a partial.

我不确定这是您想要的还是您正在寻找的,但为什么不尝试将meta标签仅放在default.blade.php 中,而不是将其放在部分中。

For example, insert in headtag of default.blade.php:

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

@yield('facebook_meta')

view_file.blade.php

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

The above approach works much better, according to me.

据我说,上述方法效果更好。

Maybe this can help you out.

也许这可以帮助你。