Register Register Member Login Member Login Member Login Forgot Password ??
PHP , ASP , ASP.NET, VB.NET, C#, Java , jQuery , Android , iOS , Windows Phone
 

Registered : 109,027

HOME > บทความจากสมาชิก > Laravel::Blade Template



 
Clound SSD Virtual Server

Laravel::Blade Template

Laravel::Blade Template มาต่อกับการจัดการ view ที่ค้างไว้คราวก่อนนะครับ บททนี้จะมาพูดถึงเรื่อง blade template

TEMPLATING
วันนั้นติดเรื่อง template ไว้นะครับ อิอิ เกือบลืมไปเลย วันนีเลยมาต่อกันเลยนะครับ แต่ก่อนอื่นเรามาเกริ่นนำกันกับเรื่องการจัดเลเอาท์ของ laravel ก่อน เริ่มแรกก็ ประกาศเลเอาท์หลักของเว็บเราก่อนนะ เราจะใช้ common ที่อยู่ตรงที่ view/layouts ประกาศไว้ที่ base controller ในโฟลเดอร์ controller นะครับ

Code (PHP)
class Base_Controller extends Controller {
    public $layout = 'layouts.common';
}


ถ้าไม่ประกาศไว้ตรงนี้ เราก็ต้องไปประกาศไว้ที่หน้า view
ตัวอย่างนี้เป็นการเรียกใช้ view จากใน method
public function action_profile()

Code (PHP)
{
    $this->layout->nest('content', 'user.profile');
}


Sections
ใช้ในกรณีเราต้องการแทรก โค้ดที่เราต้องการแทรกไปบนหน้าหลัก ใช้ section เลยครับ

Code (PHP)
<?php Section::start('scripts'); ?>
    <script src="jquery.js"></script>
<?php Section::stop(); ?>



แทรกไว้ตรง head เลย

Code (PHP)
<head>
    <?php echo Section::yield('scripts'); ?>
</head>
ทำให้สั้นลงด้วย blade template ครับ
@section('scripts')
    <script src="jquery.js"></script>
@endsection

<head>
    @yield('scripts')
</head>


Blade Template Engine
เข้าเรื่องละ
Blade ก็คือ template ที่แนบมากับตัว laravel ทำให้เราเขียน php บนหน้า html ได้ง่ายขึ้นมากๆ เลยครับ ตัวอย่างจะอยู่ตรงที่ view/home/index.blade.php การตั้งชื่อไฟล์ต้องมีคำว่า blade แทรกไว้เหมือนในตัวอย่างด้วยนะครับ

ใช้ {{ }} แทน <?php echo “” ?>;

Code (PHP)
Hello, {{ $name }}.


เหมือนใช้ {{ }} แทน แท็กเปิดปิดของ php ทีนี้ถึงจะโดนปิด short tag ไว้เราก็ไม่รำคาญละ 555

{{ Asset::styles() }}

แทรก view หนึ่งลงอีก view หนึ่ง
จะเห็นว่า laravel มี function ที่ใช้ในการแทรก view หนึ่งลงอีก view หนึ่งเยอะเลยนะครับ ตรงใน blade นี้ก็ใช้ฟังชัน render หรือ include

Code (PHP)
<h1>Profile</hi>
@include('user.profile')
@render('admin.list')
Blade comments:
{{-- This is a comment --}}

{{--
    This is a
    multi-line
    comment.
--}}


Note: ต่างจากการคอมเม้นแบบ html ตรงที่มันจะไม่โชว์บนไฟล์ html นะครับ

Blade Control Structures การจัดการข้อมูลของ blade

Code (PHP)
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 ดูอ่านง่าย เป็นสัดส่วน สบายตาขึ้นเยอะเลยครับ
Code (PHP)
<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 ข้างบนคือการแทรกตัวอย่างข้างล่างนะครับ
Code (PHP)
@layout('master')
@section('content')
    Welcome to the profile page!
@endsection


ตอนนี้เราก็เอาไปใส่ใน route ได้ละครับ

Code (PHP)
return View::make('profile');


สำคัญ: ต้องวาง @layout ไว้ที่บรรทัดแรกของหน้าเลยครับ

จบแล้วเด๋วจะมาต่อเรื่อยๆ ครับ กว่าจะจบ serie อีกยาวเบย วันนี้หยุดเลยปั่นมันมือเลย
ครั้งหน้าผมจะมาต่อเรื่อง raw query & fluent query builder จะเป็นยังไงมาติดตามอ่านนะครับ







   
Share
Bookmark.   

  By : taqman
  Article : บทความเป็นการเขียนโดยสมาชิก หากมีปัญหาเรื่องลิขสิทธิ์ กรุณาแจ้งให้ทาง webmaster ทราบด้วยครับ
  Score Rating :
  Create Date : 2013-05-12
  Download : No files
Sponsored Links
ThaiCreate.Com Forum


Comunity Forum Free Web Script
Jobs Freelance Free Uploads
Free Web Hosting Free Tools

สอน PHP ผ่าน Youtube ฟรี
สอน Android การเขียนโปรแกรม Android
สอน Windows Phone การเขียนโปรแกรม Windows Phone 7 และ 8
สอน iOS การเขียนโปรแกรม iPhone, iPad
สอน Java การเขียนโปรแกรม ภาษา Java
สอน Java GUI การเขียนโปรแกรม ภาษา Java GUI
สอน JSP การเขียนโปรแกรม ภาษา Java
สอน jQuery การเขียนโปรแกรม ภาษา jQuery
สอน .Net การเขียนโปรแกรม ภาษา .Net
Free Tutorial
สอน Google Maps Api
สอน Windows Service
สอน Entity Framework
สอน Android
สอน Java เขียน Java
Java GUI Swing
สอน JSP (Web App)
iOS (iPhone,iPad)
Windows Phone
Windows Azure
Windows Store
Laravel Framework
Yii PHP Framework
สอน jQuery
สอน jQuery กับ Ajax
สอน PHP OOP (Vdo)
Ajax Tutorials
SQL Tutorials
สอน SQL (Part 2)
JavaScript Tutorial
Javascript Tips
VBScript Tutorial
VBScript Validation
Microsoft Access
MySQL Tutorials
-- Stored Procedure
MariaDB Database
SQL Server Tutorial
SQL Server 2005
SQL Server 2008
SQL Server 2012
-- Stored Procedure
Oracle Database
-- Stored Procedure
SVN (Subversion)
แนวทางการทำ SEO
ปรับแต่งเว็บให้โหลดเร็ว


Hit Link
   







Load balance : Server 03
ThaiCreate.Com Logo
© www.ThaiCreate.Com. 2003-2024 All Rights Reserved.
ไทยครีเอทบริการ จัดทำดูแลแก้ไข Web Application ทุกรูปแบบ (PHP, .Net Application, VB.Net, C#)
[Conditions Privacy Statement] ติดต่อโฆษณา 081-987-6107 อัตราราคา คลิกที่นี่