1, Template inheritance (kế thừa giao diện).
-Mình sẽ làm ví dụ trước để các bạn dễ hiểu nhé.
Ví Dụ 1: .-Đầu tiên mình sẽ tạo ra 1 blade template có tên master.blade.php ở đường dẫn resoures\views\master.blade.php.
htmlcopy<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title> @yield('title')</title>
<link rel="stylesheet" href="">
</head>
<body>
<div class="container">
@section('content')
@show
</div>
</body>
</html>
-Và sau đó mình tạo tiếp một file home.blade.php ở đường dẫn resoures\views\home.blade.php
phpcopy@extends('master')
@section('title','đây là trang chủ')
@section('content')
<p> Đây là phần content</p>
@endsection
-Giờ tiếp đến chúng ta tạo ra một Route để gọi view.
phpcopyRoute::get('call-view', function () {
return view('home');
});
-Tiếp đó các bạn khởi động server lên và chạy đường dẫn : yourdomain/call-view lên và thử nhận xét.
=>Chú thích:
-Như ở trên mình có sử dụng các từ khóa lạ như @yield(),@section(),@extends().
–@yield(), @section-@show: có tác dụng như một biến nó được tạo ra nhằm báo cho blade template biết vị trí ý sẽ dùng để chèn nội dung cho biến đó.
–@extends(): Có tác dụng khai báo cho blade template biết là file hiện tại đang kế thừa từ file nào
. =>Thực tiễn: Như ở trong ví dụ trên: Đầu tiên thì mình khởi tạo 2 biến @yeild(‘title’),@section(‘content’) trong file master.blade.php, tiếp sau đó mình tạo tiếp file home.blade.php và dùng @extends(‘master’) để kế thừa tất cả thuộc tính từ file master.blade.php tiếp đó mình lại khai báo nội dung cho 2 biến ở file master.blade.php bằng section('tenbien','noidungngan'),@section('tenbien') noidungdai @endsection
Ví dụ 2: – Mình sẽ thay đổi code trong file master ở vd1 một chút.
htmlcopy<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title> @yield('title')</title>
<link rel="stylesheet" href="">
</head>
<body>
<div class="container">
@section('content')
<p>Dòng này là của master.blade.php</p>
@show
</div>
</body>
</html>
Và file home.php
phpcopy@extends('master')
@section('title', 'đây là trang chủ')
@section('content')
<p> Đây là phần content</p>
@endsection
Khi chạy lên như ở ví dụ 1 thì các bạn sẽ thấy source code của nó sẽ có dạng.
htmlcopy<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title> @yield('title')</title>
<link rel="stylesheet" href="">
</head>
<body>
<div class="container">
<p>Dòng này là của master.blade.php</p>
<p>Đây là phần content</p>
</div>
</body>
</html>
