This is the beginning of my master.blade.php
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>@yield('title')</title>
<meta name="description" content="@yield('meta_desc')">
If I extend this to a page where no meta_desc
is given like this:
@extends('layouts.index')
@section('title')
A nice title
@endsection
The this page has in the header a meta-tag with an empty description
<meta name="description" content="">
However I would like to skip the meta-tag completely, when no meta_desc is given. I tried to change the code in the master.blade.php
to
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>@yield('title', )</title>
@if(!empty($meta_desc))
<meta name="description" content="@yield('meta_desc')">
@endif
but then the meta-tag
<meta name="description" content="">
would be always be missing then. How can I fix it?
you can do something like this
master.blade.php
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>@yield('title')</title>
@if($metaDesc != "")
<meta name="description" content="{{ $metaDesc }}">
@endif
Controller
$metaDesc = "Testing";
return view('test', compact('metaDesc'));
test.blade.php
@extends('layouts.master')
@section('title', 'Testing')
@section('content')
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Aspernatur consectetur eligendi esse id praesentium ullam! Alias asperiores, consectetur delectus, dicta facilis impedit itaque iure magni nisi odio perferendis tenetur ut!
@endsection
so basically what i've done is passed the meta description variable to the master layout and there if the variable is "" then we don't show the <meta>
at all..
Feel free to reach out if any doubts..