laravel之blade模板标签的含义

本文介绍了laravel框架中blade模板的使用,包括父布局与子布局的关系,@section、@yield、@extends、@parent等关键标签的含义和用法,并通过实例展示了@show、@stop、@append和@overwrite在内容输出和数据添加上的不同操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

laravel之blade模板标签的含义

父布局-layout.blade.php
<div class="header">
    @section('header')
        header
    @show
</div>
<div class="main">
    <div class="sidebar">
        @section('sidebar')
            sidebar
        @show
    </div>
    <div class="content">
        @yield('content','主要内容区域')
        content
    </div>
</div>
<div class="footer">
    @section('footer')
        footer
    @show</div>
</body>
</html>
子布局-son.blade.php
@extends('layouts')

@section('header')
    @parent
    heihei
@stop

@section('sidebar')
    边栏
@stop

1.@section(‘name’): 块,可@parent继承使用父layout中定义的内容
2.@yield(‘name’): 字段,@parent无效,要么内容是子layout的内容,要么是父layout的内容,当子layout未定义时,显示父layout的内容
3.@extends(‘layouts’),继承模板
4.@parent,继承父模板的内容

@show与@stop,@overwrite,@append的使用

@show:将内容输出到界面,在子模板中可以继承使用
@stop:在模板中不继承可以使用
列子:

//parent
@section('main')
    main
@show
@section('footer')
    footer
@stop

//son
@extends('parent')

@section('main')
    son-main
@stop

@section('footer')
    son-footer
@stop

只会显示son-main,son-footer不会显示

@append: 还可以用同一@section(‘name’),添加数据

@section('main')
        hehe
@append
@section('main')
        xixi
@append

输出hehe xixi

@overwrite: 还可以用同一@section(‘name’),添加数据

@section('main')
        hehe
@append
@section('main')
        xixi
@overwrite

输出xixi

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值