doutingyou2198 2017-10-03 09:50
浏览 29
已采纳

Laravel Blade:仅在扩展时给出变量时显示元标记

This is the beginning of my master.blade.php

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title>@yield('title')</title>
  6. <meta name="description" content="@yield('meta_desc')">

If I extend this to a page where no meta_desc is given like this:

  1. @extends('layouts.index')
  2. @section('title')
  3. A nice title
  4. @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

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title>@yield('title', )</title>
  6. @if(!empty($meta_desc))
  7. <meta name="description" content="@yield('meta_desc')">
  8. @endif

but then the meta-tag

<meta name="description" content="">

would be always be missing then. How can I fix it?

  • 写回答

1条回答 默认 最新

  • duanjiu2701 2017-10-05 00:30
    关注

    you can do something like this

    master.blade.php

    1. <!DOCTYPE html>
    2. <html>
    3. <head>
    4. <meta charset="utf-8">
    5. <title>@yield('title')</title>
    6. @if($metaDesc != "")
    7. <meta name="description" content="{{ $metaDesc }}">
    8. @endif

    Controller

    1. $metaDesc = "Testing";
    2. return view('test', compact('metaDesc'));

    test.blade.php

    1. @extends('layouts.master')
    2. @section('title', 'Testing')
    3. @section('content')
    4. 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!
    5. @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..

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部