Laravel 拉维刀片:嵌套部分在这里做什么?
我不知道把一个部分放在另一个部分里面做什么。在我正在阅读的Laravel项目中,Blade文件的代码如下:Laravel 拉维刀片:嵌套部分在这里做什么?,laravel,laravel-blade,Laravel,Laravel Blade,我不知道把一个部分放在另一个部分里面做什么。在我正在阅读的Laravel项目中,Blade文件的代码如下: login.blade.php: @extends('layout') @section('content') @section('title', 'Log in') Lots of content. @endsection 在上面,在内容部分中包含标题部分有什么意义?如果标题部分位于以下位置之外,会有什么不同: @extends('layout') @section('title',
login.blade.php:
@extends('layout') @section('content')
@section('title', 'Log in')
Lots of content.
@endsection
在上面,在内容部分中包含标题部分有什么意义?如果标题部分位于以下位置之外,会有什么不同:
@extends('layout')
@section('title', 'Log in')
@section('content')
Lots of content.
@endsection
我进行了测试,两者都产生了相同的输出(HTML源代码)
layout.blade.php
<head><title>@yield('title')</title></head>
<body>@yield('content')</body>
@yield('title'))
@产量(‘含量’)
layout.blade.php是否会产生不同的输出?为了可读性,最好将它们放在不同的行上 第一节仅将“登录”放在“标题”所在的位置:
第二节有开头和结尾,并且是自描述性的。节指令只是将@section和@endsection中的所有内容复制到扩展模板上的名称@yield placeholder
这意味着它不考虑它的放置位置,但是为了清晰和可读性,第二个例子是正确的结构。 例如,这表明指令的顺序并不重要
在welcome.blade.php中@extends('default')
@section('a')
This is a
@section('c', 'This is c')
@section('b')
This is b
@section('d', 'This is d')
@endsection
@endsection
@yield('c')
@yield('a')
@yield('b')
@yield('d')
在default.blade.php中
@extends('default')
@section('a')
This is a
@section('c', 'This is c')
@section('b')
This is b
@section('d', 'This is d')
@endsection
@endsection
@yield('c')
@yield('a')
@yield('b')
@yield('d')
输出
This is c This is a This is b This is d
最佳做法是通过打开和关闭每个块使代码更具可读性:
@extends('default')
@section('a')
This is a
@endsection
@section('c', 'This is c')
@section('b')
This is b
@endsection
@section('d', 'This is d')
section指令只需将
@section
和@endsection
中的任何内容复制到扩展模板上的名称@yield
占位符。这意味着它不考虑它放置在哪里,但是为了清晰和可读性,第二个例子是正确的结构谢谢,但是下面的问题和它的答案让我觉得也许有一些使用嵌套部分: