2016-01-08 241 views
4

我試圖將說明和關鍵字添加到我的Laravel頁面。將meta標籤添加到laravel頁面

這種方式有效嗎?或者有什麼問題?

@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') 

回答

9

您是否在擴展另一個使用所有這些模板的模板sections?他們不會自己工作,他們需要在另一個模板中填充佔位符。

它應該是這樣的:

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

然後你的模板應擴展其他的模板。

@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') 

至少,這是我閱讀他們的文檔:https://laravel.com/docs/5.2/blade

1

您可以創建只有一個,我以下提到的所有這些重要的標記部分。並在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