1,Tạo blade template trong Laravel.
-Để tạo một blade template trong Laravel thì trước hết các bạn phải hiểu blade template bản chất nó là một view trong Laravel nên các bạn phải đặt nó trong thư mục resources/views (view trong laravel). Và tên file của blade template phải kết thúc bằng .blade.php
VD: mình sẽ tạo 1 blade template toidicode.blade.php trong thư mục resources/views
htmlcopy<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Blade template</title>
<link rel="stylesheet" href="">
</head>
<body>
<h1>Chào mừng các bạn đã đến toidicode.com</h1>
</body>
</html>
2,Sử dụng blade template.
Hiển thị dữ liệu trong blade template.
-Trong PHP khi muốn in dữ liệu ra màn hình các bạn có thể dụng echo,printf,… Nhưng ở đây khi các bạn sử dụng blade template thì sẽ có 2 cách để in dữ liệu ra màn hình khác.
-Sử dụng {{}}.
phpcopy{{ 'bien' }}
-Thẻ này có tác dụng in ra dữ liệu dưới dạng thô . VD:
phpcopy{{ '<b>toidicode.com</b>' }}
KQ:
| <b>toidicode.com</b> |
-Sử dụng
phpcopy{!! 'bien' !!}
-Thẻ này cho in ra dự liệu có appen HTML. VD:
phpcopy{!! '<b>toidicode.com</b>' !!}
KQ:
| toidicode.com |
Dữ liệu mặc định trong blade template.
-Để sử dụng dữ liệu mặc định trong blade template chúng ta có thể dùng OR.
VD: in ra bến $a nếu không có thì là 5.
phpcopy{{ $a or 5 }}
Vòng lặp trong blade template.
-For:
phpcopy@for($i = 0; $i < 10; $i++)
In ra giá trị {{ $i }} <br/>
@endfor
-Foreach:
phpcopy@foreach($users as $user)
<p>This is user {{ $user->id }}</p>
@endforeach
-Forelse:
+ Lặp dữ liệu nếu trống thì…
phpcopy@forelse($users as $user)
<li>{{ $user->name }}</li>
@empty
<p>No users</p>
@endforelse
-while:
phpcopy@while (true)
<p>I'm looping forever.</p>
@endwhile
Câu lệnh rẽ nhánh trong blade template engine.
-if-else:
phpcopy@if(count($records) === 1)
I have one record!
@elseif (count($records) > 1)
I have multiple records!
@else
I don't have any records!
@endif
