父模板@yield(‘part1’),
子模板@extends父模板,@section(‘part1’)和@endsection设置section。
@include(‘test.v1’),v1.blade.php里有变量,所以v2里写了php代码。
//resources\views\test\v2.blade.php
@extends('mytemplate.parent')
@section('part1')
<div>666</div>
@endsection
@section('part2')
<div>777</div>
@endsection
<?php
$date = date("Y/m/d H:i:s", time());
$a1y = strtotime('+1 year');
$arr1 = [['id' => 1, 'name' => 'n1'], ['id' => 2, 'name' => 'n2'], ['id' => 3, 'name' => 'n3']];
?>
@include('test.v1')
app\Http\Controllers\TestViewController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class TestViewController extends Controller
{
public function v1(){
$date=date("Y/m/d H:i:s", time());
$a1y=strtotime('+1 year');
$arr1=[['id'=>1,'name'=>'n1'],['id'=>2,'name'=>'n2'],['id'=>3,'name'=>'n3']];
// return view('test.v1', ['date'=>$date,'a1y'=>$a1y]);
return view('test.v1', compact('date','a1y','arr1'));
}
public function v2(){
return view('test.v2');
}
}
//routes\web.php
Route::get('/v1', [TestVIewController::class, 'v1']);
Route::get('/v2', [TestVIewController::class, 'v2']);
//resources\views\mytemplate\parent.blade.php
<h1>上部</h1>
@yield('part1')
<h1>下部</h1>
@yield('part2')
浏览器访问http://localhost:3000/public/v2,显示如下:
现在时间:2022/07/24 06:53:37
一年后时间:2023-07-24 06:53:37
1---n1
2---n2
3---n3
0 can divide by 2
1 cannot divide by
2 can divide by 2
3 can divide by 3
4 can divide by 2
5 cannot divide by
6 can divide by 2
7 cannot divide by
8 can divide by 2
9 can divide by 3
10 can divide by 2
上部
666
下部
777