laravel 模板 引用 include extends section

9 篇文章 0 订阅

父模板@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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值