Laravel 5 :(4)Blade Template
Laravel 範例實作
此筆記主要紀錄學習以下 Laravel 的範例教學
樣板繼承
樣板繼承(Template inheritance)讓我們預先定義一個 Master layout
而其他頁面可以繼承此樣板,簡單的說就是類似切版
先切出每一頁都會用到的 layout (類如 Header,footer)
之後每一個 web page 去繼承此 layout 就好,類似 include 的概念
Master layout
1.在resources\views建立layouts資料夾
2.在layouts資料夾中建立master.blade.php
3.master.blade.php內容如下
1 | <html> |
擴展 Master layout
- 建立
/resources/views/page.blade.php
1 | @extends('layouts.master') |
解釋如下1
2
3
4
5
6@extends('layouts.master') 擴展 Master layout
@section('title', 'Page Title') 將 title section 設定為 Page Title
@section('sidebar') 定義 sidebar section
@endsection sidebar section 結束
@section('content') 定義 content section
@endsection content section 結束
2.打開 /routes/web.php,後面加上
1 | Route::get('/blade', function () { |
3.瀏覽http://localhost/larashop/public/blade,會出現
1 | This is appended to the master sidebar. // page.blade.php |
你可以打開原始碼來看
1 | <html> |
在 blade template 顯示變數
1.修改/routes/web.php
1 | Route::get('/blade', function () { |
2.修改page.blade.php
1 | @extends('layouts.master') |
條件判斷
1.打開web.php,修改為
1 | Route::get('/blade', function () { |
2.修改page.blade.php1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20@extends('layouts.master')
@section('title', 'Page Title')
@section('sidebar')
<p>This is appended to the master sidebar.</p>
@endsection
@section('content')
<h2>{{$name}}</h2>
<p>This is my body content.</p>
<h2>If Statement</h2>
@if ($day == 'Friday')
<p>Time to party</p>
@else
<p>Time to make money</p>
@endif
@endsection
迴圈
1.打開web.php,修改為
1 | Route::get('/blade', function () { |
2.修改page.blade.php1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25@extends('layouts.master')
@section('title', 'Page Title')
@section('sidebar')
<p>This is appended to the master sidebar.</p>
@endsection
@section('content')
<h2>{{$name}}</h2>
<p>This is my body content.</p>
<h2>If Statement</h2>
@if ($day == 'Friday')
<p>Time to party</p>
@else
<p>Time to make money</p>
@endif
<h2>Foreach Loop</h2>
@foreach ($drinks as $drink)
{{$drink}} <br>
@endforeach
@endsection
呼叫 PHP Function
1.修改page.blade.php1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28@extends('layouts.master')
@section('title', 'Page Title')
@section('sidebar')
<p>This is appended to the master sidebar.</p>
@endsection
@section('content')
<h2>{{$name}}</h2>
<p>This is my body content.</p>
<h2>If Statement</h2>
@if ($day == 'Friday')
<p>Time to party</p>
@else
<p>Time to make money</p>
@endif
<h2>Foreach Loop</h2>
@foreach ($drinks as $drink)
{{$drink}} <br>
@endforeach
<h2>Execute PHP Function</h2>
<p>The date is {{date(' D M, Y')}}</p>
@endsection
整合美術樣板
1.建立/resources/views/layouts/layout.blade.php
1 | <!--Header HTML code from E-Shopper Template--> |
解釋如下
1 | {{asset('css/name.css')}} 會呼叫 http://localhost/larashop/public/css/name.css |
2.子頁面
在/resources/views/底下建立
1 | blog.blade.php |
每一個檔案內容如下
1 | @extends('layouts.layout') |
3.修改 controller
打開app/Http/Controllers/Front.php,修改為
1 | class Front extends Controller { |
4.複製資源檔案
將下載下來的1
2
3
4css
fonts
images
js
覆蓋到public資料夾下