Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

blade template, @yield() in @yield

This is my current implementation of the Open Graph tags using Laravel 5:

app.blade.php

<title>@yield('title')</title>
<meta property="og:title" content="@yield('og-title', 'DEFAULT PAGE TITLE')">
<meta property="og:image" content="@yield('og-image', 'default.png')">
<meta property="og:url" content="@yield('og-url', '{{ Request::url() }}')">
<meta property="og:site_name" content="SITE NAME">

page.blade.php

@extends('app')
@section('title', $article->title . ' | SITE NAME')
@section('og-title', $article->title)
@section('og-image', secure_url('img/news/' . $article->image .'.png'))

It works for the most part, but I have a couple inquiries:

  1. Is there a way to use @yield() in @yield?

    I tried <meta property="og:title" content="@yield('og-title', @yield('title'))"> but it did not work.

  2. How can I get the current SECURE url of the page?

    {{ Request::url() }} returns http://example.com/page, but I want https://example.com/page

As a meta, please let me know if you have any suggestions to improve my current method of OG.

like image 972
O P Avatar asked May 24 '15 06:05

O P


1 Answers

There is an easy way to achieve what you want. What you can do is to move the OG meta tags html to partial view, to which you can pass parameters on each page as you wish. For example in your master layout just create section for the og tags:

app.blade.php

<title>@yield('title')</title>
@section('ogtags')

@show

Now create your partial view let's call it:

og_tags.blade.php

<meta property="og:title" content="{{ $title or 'DEFAULT PAGE TITLE' }}">
<meta property="og:image" content="{{ $image or 'default.png' }}">
<meta property="og:url" content="{{ isset($url) ? $url : str_replace('http://', 'https://', Request::url()) }}">

So now in you views you can easily add og-tags like this:

home.blade.php

@extends('app')

@section('ogtags')
    @include('og_tags', ['title' => 'my title', 'image' => 'my-image.png'])
@stop

@section('content')
     your content here
@stop

For the secure url question, Request::url() returns the current url of the page, if it is via HTTPS it will start with https:// otherwise it will start with http://. So this is why I just replace it to always be https

str_replace('http://', 'https://', Request::url())

If you wish to always have og_tags (in case your view does not define this section and you want to show default ones) you can just modify your app.blade.php like this:

<title>@yield('title')</title>
@section('ogtags')
    @include('og_tags')
@show

This is the cool part of Blade that you can split everything in smaller parts, then include them with dynamic parameters, or just create view composers to handle the data.

like image 180
Sh1d0w Avatar answered Sep 26 '22 07:09

Sh1d0w