通过yield向laravel部分传递参数

问题描述 投票:0回答:2

我的刀片视图有问题。我正在创建一个变量,并且有一个 @yield,我在另一个页面中设置其内容。

layout.blade.php:

@php $x = 5 @endphp
@yield('content')

我想通过yield 传递这个变量,以便可以在其他页面中访问它。

content.blade.php:

@extends('layout')
   @section('content')
      {{$x}}
   @endsection

内容部分处于循环中,并且 x 的值正在更改,因此无法从控制器传递。 那么有没有办法将数据从@yield传递到@section?

php laravel laravel-blade
2个回答
0
投票

你应该在layout.blade.php中有一个Html结构才能在content.blade.php中看到它

例如,如果您想在标题中看到它:

    @php $x = '5'; @endphp

    <html>
      <head>    
        <title>@yield('content', $x)</title>
      </head>

    <body></body>
    </html>

在 content.blade.php 中:

@extends('layout')

0
投票

在 app.layout 文件中声明变量 @php $date = date('Y-m-d H:i:s') @endphp

然后在 Blade 文件中的大括号内使用它,就像 nav.blade.php 中一样 日期:{{$日期}}

© www.soinside.com 2019 - 2024. All rights reserved.