Html 将元标记添加到 Laravel 页面

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34676729/
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-08-29 18:44:43  来源:igfitidea点击:

Add meta tags to laravel page

htmllaravel-5laravel-5.1meta-tags

提问by Oleg Grytsenko

I'm trying to add description and keywords to my Laravel pages.

我正在尝试向我的 Laravel 页面添加描述和关键字。

Is this way working? Or there is something wrong?

这种方式行得通吗?或者有什么问题?

@section('title')
{{trans('strings.About')}}
@stop
@section('description', 'Share text and photos with your friends and have fun')
@section('keywords', 'sharing, sharing text, text, sharing photo, photo,')
@section('robots', 'index, follow')
@section('revisit-after', 'content="3 days')

回答by Adam Taylor

Are you extending another template that uses all these sections? They won't work on their own, they need to populate a placeholder in another template.

您是否正在扩展另一个使用所有这些的模板sections?他们不会自己工作,他们需要在另一个模板中填充占位符。

It should be something like:

它应该是这样的:

<!-- layouts.master -->
<html>
    <head>
        <title>App Name - @yield('title')</title>
        <meta name="description" content="@yield('description')">
        <meta name="keywords" content="@yield('keywords')">
        <!-- etc -->
    </head>
    <body>
        ...
    </body>
</html>

And then your template should extend the other template.

然后你的模板应该扩展另一个模板。

@extends('layouts.master')
@section('title')
{{trans('strings.About')}}
@stop
@section('description', 'Share text and photos with your friends and have fun')
@section('keywords', 'sharing, sharing text, text, sharing photo, photo,')
@section('robots', 'index, follow')
@section('revisit-after', 'content="3 days')

At least, that is how I read their documentation: https://laravel.com/docs/5.2/blade

至少,这就是我阅读他们文档的方式:https: //laravel.com/docs/5.2/blade

回答by B. R. N. Rajoriya

You can create only one section with all these important tags that I have mentioned below. And @yieldthis section in app layout <head>section of HTML code.

您只能使用我在下面提到的所有这些重要标签创建一个部分。而@yield这部分的应用程序布局<head>的HTML代码段。

@section('meta_tags')
    @if($obj)
        <title>{{$obj->title}} - {{env('SITE_URL', 'Site Name')}}</title>
        <meta name='description' itemprop='description' content='{{$obj->description}}' />
        <?php $tags = implode(',', $obj->tags); ?>
        <meta name='keywords' content='{{$tags}}' />
        <meta property='article:published_time' content='{{$obj->created_at}}' />
        <meta property='article:section' content='event' />

        <meta property="og:description" content="{{$obj->description}}" />
        <meta property="og:title" content="{{$obj->title}}" />
        <meta property="og:url" content="{{url()->current()}}" />
        <meta property="og:type" content="article" />
        <meta property="og:locale" content="en-us" />
        <meta property="og:locale:alternate" content="en-us" />
        <meta property="og:site_name" content="{{env('SITE_URL', 'Site Name')}}" />
        @foreach($obj->images as $image)
            <meta property="og:image" content="{{$image->url}}" />
        @endforeach
        <meta property="og:image:url" content="{{obj->image}}" />
        <meta property="og:image:size" content="300" />

        <meta name="twitter:card" content="summary" />
        <meta name="twitter:title" content="{{$obj->title}}" />
        <meta name="twitter:site" content="@BrnBhaskar" />
    @endif
@endsection