我的刀片视图有问题。我正在创建一个变量,并且有一个 @yield,我在另一个页面中设置其内容。
layout.blade.php:
@php $x = 5 @endphp
@yield('content')
我想通过yield 传递这个变量,以便可以在其他页面中访问它。
content.blade.php:
@extends('layout')
@section('content')
{{$x}}
@endsection
内容部分处于循环中,并且 x 的值正在更改,因此无法从控制器传递。 那么有没有办法将数据从@yield传递到@section?
你应该在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')
在 app.layout 文件中声明变量 @php $date = date('Y-m-d H:i:s') @endphp
然后在 Blade 文件中的大括号内使用它,就像 nav.blade.php 中一样 日期:{{$日期}}