Laravel-使用@section动态设置元标记 [英] Laravel - set meta tag dynamically with @section

查看:169
本文介绍了Laravel-使用@section动态设置元标记的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现Facebook共享按钮功能,以便在用户点击共享按钮时捕获元标签信息.

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

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

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

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

And i have a 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>

因此,当我浏览到一些网址说www.example.com/details时,default.blade.php将从该details.blade中产生内容.现在,我想更改facebook meta标签的属性,因此我在详细信息页面中做了以下操作.

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.

@section('facebook_share_image', 'This is a description')
@section('facebook_share_description', 'This is an individual page title')

代码工作正常,但是我想从我的视图数据中渲染它.类似于@section('facebook_share_description', {{ $data->description }})

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

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

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

推荐答案

我不确定这是您想要的还是正在寻找的东西,但是为什么不尝试将meta标记放在默认值中.blade.php ,而不是将其部分放置.

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

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

For example, insert in head tag of default.blade.php:

@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

根据我的说法,上述方法效果更好.

The above approach works much better, according to me.

也许这可以帮助您.

这篇关于Laravel-使用@section动态设置元标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆