TEMPLATING
วันนั้นติดเรื่อง template ไว้นะครับ
อิอิ เกือบลืมไปเลย วันนีเลยมาต่อกันเลยนะครับ
แต่ก่อนอื่นเรามาเกริ่นนำกันกับเรื่องการจัดเลเอาท์ของ laravel ก่อน
เริ่มแรกก็
ประกาศเลเอาท์หลักของเว็บเราก่อนนะ เราจะใช้ common ที่อยู่ตรงที่
view/layouts ประกาศไว้ที่
base controller ในโฟลเดอร์
controller นะครับ
class Base_Controller extends Controller {
public $layout = 'layouts.common';
}
ถ้าไม่ประกาศไว้ตรงนี้
เราก็ต้องไปประกาศไว้ที่หน้า view
ตัวอย่างนี้เป็นการเรียกใช้
view จากใน method
public function action_profile()
{
$this->layout->nest('content', 'user.profile');
}
ใช้ในกรณีเราต้องการแทรก
โค้ดที่เราต้องการแทรกไปบนหน้าหลัก ใช้ section เลยครับ
<?php Section::start('scripts'); ?>
<script src="jquery.js"></script>
<?php Section::stop(); ?>
แทรกไว้ตรง head เลย
<head>
<?php echo Section::yield('scripts'); ?>
</head>
ทำให้สั้นลงด้วย blade template ครับ
@section('scripts')
<script src="jquery.js"></script>
@endsection
<head>
@yield('scripts')
</head>
Blade ก็คือ template ที่แนบมากับตัว
laravel ทำให้เราเขียน php บนหน้า html ได้ง่ายขึ้นมากๆ
เลยครับ ตัวอย่างจะอยู่ตรงที่ view/home/index.blade.php การตั้งชื่อไฟล์ต้องมีคำว่า
blade แทรกไว้เหมือนในตัวอย่างด้วยนะครับ
ใช้ {{ }} แทน <?php echo “” ?>;
Hello, {{ $name }}.
เหมือนใช้ {{ }} แทน แท็กเปิดปิดของ php ทีนี้ถึงจะโดนปิด short tag ไว้เราก็ไม่รำคาญละ 555
{{ Asset::styles() }}
แทรก view หนึ่งลงอีก view หนึ่ง
จะเห็นว่า
laravel มี function ที่ใช้ในการแทรก
view หนึ่งลงอีก view หนึ่งเยอะเลยนะครับ
ตรงใน blade นี้ก็ใช้ฟังชัน render หรือ include
<h1>Profile</hi>
@include('user.profile')
@render('admin.list')
Blade comments:
{{-- This is a comment --}}
{{--
This is a
multi-line
comment.
--}}
Note: ต่างจากการคอมเม้นแบบ html ตรงที่มันจะไม่โชบนไฟล์ html นะครับ
For Loop:
@for ($i = 0; $i <= count($comments); $i++)
The comment body is {{ $comments[$i] }}
@endfor
Foreach Loop:
@foreach ($comments as $comment)
The comment body is {{ $comment->body }}.
@endforeach
While Loop:
@while ($something)
I am still looping!
@endwhile
If Statement:
@if ( $message == true )
I'm displaying the message!
@endif
If Else Statement:
@if (count($comments) > 0)
I have comments!
@else
I have no comments!
@endif
Else If Statement:
@if ( $message == 'success' )
It was a success!
@elseif ( $message == 'error' )
An error occurred.
@else
Did it work?
@endif
For Else Statement:
@forelse ($posts as $post)
{{ $post->body }}
@empty
There are not posts in the array!
@endforelse
Unless Statement:
@unless(Auth::check())
Login
@endunless
// Equivalent to...
<?php if ( ! Auth::check()): ?>
Login
<?php endif; ?>
Blade Layouts
การใช้
blade template ทำให้หน้า html ดูอ่านง่าย
เป็นสัดส่วน สบายตาขึ้นเยอะเลยครับ
<html>
<ul class="navigation">
@section('navigation')
<li>Nav Item 1</li>
<li>Nav Item 2</li>
@yield_section
</ul>
<div class="content">
@yield('content')
</div>
</html>
Content ข้างบนคือการแทรกตัวอย่างข้างล่างนะครับ
@layout('master')
@section('content')
Welcome to the profile page!
@endsection
ตอนนี้เราก็เอาไปใส่ใน
route ได้ละครับ
return View::make('profile');
สำคัญ: ต้องวาง @layout ไว้ที่บรรทัดแรกของหน้าเลยครับ
ไม่มีความคิดเห็น:
แสดงความคิดเห็น