2016-12-03 102 views
1

我正在嘗試文件中的一個yield,但它不起作用。Laravel刀片未顯示內容

這是我index.blade.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html lang="en"> 
<head> 
    <meta http-equiv="X-UA-Compatible" content="IE=edge"> 
    <meta name="viewport" content="width=device-width, initial-scale=1"> 
    <meta name="token" content="{{ csrf_token() }}"> 
    <title>Forum</title> 
    <link href="https://fonts.googleapis.com/css?family=Lato:100,200,300,400" rel="stylesheet" type="text/css"> 
    <link href='https://fonts.googleapis.com/css?family=Roboto:400,500,700' rel='stylesheet' type='text/css'> 
    <link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet"> 

    <link href="/css/bulma.css" rel='stylesheet' type='text/css'> 
    <link href="/css/all.css" rel='stylesheet' type='text/css'> 
</head> 
<body> 
<div id="app"> 
    @include('shared.menu.menu') 
    @yield('content') 
</div> 
<script src="/js/app.js"></script> 
</body> 
</html> 

這是我login.blade.php

@extends('index') 
    @section('content') 
     @yield('shared.bar.bar') 
    @stop 

所以導航欄顯示在這一點上。但酒吧不是!當我替換:@yield('shared.bar.bar')test,test顯示出來。這是shared/bar/bar.blade.php

@section('bar') 
<section class="hero is-primary"> 
    <div class="hero-body"> 
     <div class="container"> 
      <h1 class="title"> 
       test 
      </h1> 
      <h2 class="subtitle"> 
       test 
      </h2> 
     </div> 
    </div> 
</section> 
@stop 

我在做什麼錯?是否也可以將變量傳遞給酒吧?所以我可以在每個頁面上顯示另一個titlesubtitle

- 編輯 -

@section('bar') 
<section class="hero is-primary"> 
    <div class="hero-body"> 
     <div class="container"> 
      <h1 class="title"> 
       {{ $title }} 
      </h1> 
      <h2 class="subtitle"> 
       {{ $subtitle }} 
      </h2> 
     </div> 
    </div> 
</section> 
@stop 

回答

1

如果你是顯示所有的代碼,看起來像你的收益率是

@yield('shared.bar.bar') 

而且YOUT部分

@section('bar') 

當它應該be

@section('shared.bar.bar') 

或者你要改變你的收益率

@yield('bar') 

因爲@yield@section只是字符串,它們是不相關的文件,如@include

,看起來像這樣的文件也是錯誤的:

@extends('index') 

@section('content') 
    @yield('shared.bar.bar') 
@stop 

@yield是你應該開始只在主佈局文件中使用,因爲它會導致混亂和艱難,更難已瞭解發生了什麼。所以,除非你有一個部分'shared.bar。酒吧」填補了這一@yield,你應該這樣做

@extends('index') 

@section('content') 
    <form> 
     .... your login form 
    </form> 
@stop 

@extends('index') 

@section('content') 
    @include('shared.bar.bar') 
@stop 

@extends('index') 

@section('content') 
    @include('shared.bar.bar') 

    <form> 
     .... your login form 
    </form> 
@stop 

要有標題通路部分,你也可以改變做:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html lang="en"> 
<head> 
    <title>@yield('title')</title> 
</head> 

<h1 class="title"> 
    @yield('title') 
</h1> 

而且您的登錄刀片的文件,你可以做

@extends('index') 

// This will tell the Blade Compiler to replace every 
// occurence of @yield('title') 
@section('title') 
    User Login 
@stop 

@section('content') 
    @include('shared.bar.bar') 

    <form> 
     .... your login form 
    </form> 
@stop 
+0

嗨,我都稱之爲'''bar'''它仍然沒有工作。另外當我把它們都叫做'''shared.bar.bar'''。 – Jamie

+0

看起來你錯誤地構建了它們,看看我的編輯。 –

+0

感謝您的幫助。我也想傳遞2個參數(參見上面的我的編輯)。但是'''include'''不可能。這就是我使用yield的原因。 – Jamie