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

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

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?

  • 写回答

1条回答 默认 最新

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

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

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

报告相同问题?

悬赏问题

  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用