Sunday, May 24, 2015

Contoh penggunaan Facade Blade::

1. Cara nak extend blade layout :

cipta fail app/views/layouts/main.blade.php sebagain kerangka utama :

  1. <html>
  2. <head>
  3. {{-- Common Header Stuff Here --}}
  4. </head>
  5. <body>
  6. <div class="navigation">
  7. @section('navigation')
  8. <a href="/">Home</a>
  9. <a href="/contact">Contact</a>
  10. @show
  11. </div>
  12. <div class="container">
  13. @yield('content')
  14. </div>
  15. </body>
  16. </html>


dalam fail view rujukan :

  1. @extends('layouts.main')
  2.  
  3. @section('content')
  4. <p>
  5. Here's your content
  6. </p>
  7. @stop


2.  Output Content - guna @yield  :

@section('bottom')
  This is the bottom
@stop
@section('top')
  This is the top
@stop 
 
@yield('top')
@yield('bottom')
 
 
papar teks default :
 
@yield('content','No content')
 
- bila view tanpa @section('content') , teks 'No content'  akan dipaparkan
 
contoh :
 
@yield('content', '<span style="background:red">MISSING CONTENT</span>') 


3. Papar pembolehubah dalam Blade :

Hello {{ $name }}
 
-untuk escap sequence :
 
Hello {{{ $name }}}
- fungsi htmlentities() sebelum output ke web
 
- untuk guna default :
 
Hello {{{ $name or 'No name' }}}
 
- nak papar kurungan {
 
@{{ This is displayed WITH the braces on each side }}
 
4.    @if  Blade :

@if ($age < 6)
    Get outta here kid.
@elseif ($age < 21)
    You can't buy booze in the United States.
    Ha ha.
@else
    Buy me a drink?
@endif


5.  @unless Blade :

@unless ($age >= 18)
    You can't vote.
@endunless
 
Nota : sama dengan if ( ! condition)
 
 
6. Komen dalam Blade :

{{-- of course, you can have single line comments too --}}
 
 
7.  @for Blade :

  @for ($i = 1; $i <= 10; $i++)
    <p>{{ $i }}</p>
  @endfor
 
 
8.    @foreach  Blade :

  <p>A list of items.</p>
  <ul>
    @foreach ($items as $item)
      <li>{{{ $item }}</li>
    @endforeach
  </ul>
 
atau :
 
  <p>A dictionary.</p>
  <dl>
    @foreach ($dict as $word => $meaning)
      <dt>{{{ $word }}}</dt>
      <dd>{{{ $meaning }}</dd>
    @endforeach
  </dl>
 
 

9.    @while Blade :

    @while ($item = array_pop($items))
      <li>{{{ $item }}</li>
    @endforeach
 
 
10.    @include Blade :

@include('common-header')
 
- ini akan include : views/common-header.php atau views/common-header.blade.php 

guna pembolehubah :

contoh :

bina fail :

views/partials/item-display.blade.php

<div>
    Name: {{ $item->name }}<br>
    Description: {{{ $item->description }}}
</div>
 
 
  @foreach ($items as $foo)
    @include('partials.item-display', array('item' => $foo))
  @endforeach
 
11.    Render View untuk item dalam collection:

@each('items.single', $items, 'item')
 
ini bersamaan :
 
Items:
@foreach ($items as $item)
    @include('items.single', ['item' => $item])
@endforeach
 
 
contoh lain :
 
@each('items.single', $items, 'item', 'raw|There are no items')  


12.    Yield seksyen Blade - guna @show:

@section('one')
  Something here
  But we'll end it, so it won't output
@stop

@section('two')
  Something else
@show
 

13.    Injek Content dalam seksyen Blade :   

@section('movies')
    I Am Legend
@stop
 
atau 
 
@section('movies', 'I Am Legend')
 
paparkan dengan :
 
@yield('movies')
 
lain arahan dalam section :
 
  1.  @stop - berhenti injek.
  2.  @endsection - sama dengan @stop.
  3.  @show - yield seksyen dalam blade.
  4.  @append - berhenti injek, dan tambah.
  5.  @overwrite - berhenti injek dan overwrite.
 

 14.   @stop

@section('test')
   one
@stop
@section('test')
   two
@stop
@yield('test')

hasil :

one


15.    @append

@section('test')
   one
@stop
@section('test')
   two
@append
@yield('test')
 
hasilnya :
 
one
two
  

16.    @Overwrite :

@section('test')
   one
@stop
@section('test')
   two
@overwrite
@yield('test')
 
hasilnya :
 
two 

17.   Parent Section dalam Blade :

  1. @section('test')
  2. Bottom
  3. @stop
  4.  
  5. {{-- Later --}}
  6. @section('test')
  7. Top
  8. @show
hasil :

Top
Bottom
   

18. Tag content dalam Blade :
edit app/start/global.php
Blade::setContentTags('[%', '%]');
 
-nak guna tag '[%' instead of {{
 
Blade::setContentTags('[-%', '%-]', true);
 
  


19.    Tag Escaped Content Blade

Blade::setContentTags('[{{', '}}]');
 

20.    Output Translation dalam Blade :

@lang('messages.welcome')
 
- untuk placehorder : 
 
@lang('messages.welcome', ['name' => $name])
 
  

21.    Output Translation dengan 'plural' dalam Blade :

@choice('messages.items', 1)
 
- untuk palceholder :
 
@choice('message.items', 3, ['type' => 'widget']);
 
  

22.   Arahan Blade :



  • {{ $var }} - Echo content
  • {{ $var or 'default' }} - Echo content with a default value
  • {{{ $var }}} - Echo escaped content
  • {{-- Comment --}} - A Blade comment
  • @extends('layout') - Extends a template with a layout
  • @if(condition) - Starts an if block
  • @else - Starts an else block
  • @elseif(condition) - Start a elseif block
  • @endif - Ends a if block
  • @foreach($list as $key => $val) - Starts a foreach block
  • @endforeach - Ends a foreach block
  • @for($i = 0; $i < 10; $i++) - Starts a for block
  • @endfor - Ends a for block
  • @while(condition) - Starts a while block
  • @endwhile - Ends a while block
  • @unless(condition) - Starts an unless block
  • @endunless - Ends an unless block
  • @include(file) - Includes another template
  • @include(file, ['var' => $val,...]) - Includes a template, passing new variables.
  • @each('file',$list,'item') - Renders a template on a collection
  • @each('file',$list,'item','empty') - Renders a template on a collection or a different template if collection is empty.
  • @yield('section') - Yields content of a section.
  • @show - Ends section and yields its content
  • @lang('message') - Outputs message from translation table
  • @choice('message', $count) - Outputs message with language pluralization
  • @section('name') - Starts a section
  • @stop - Ends section
  • @endsection - Ends section
  • @append - Ends section and appends it to existing of section of same name
  • @overwrite - Ends section, overwriting previous section of same name

23.    Bina pembolehubah dalam Blade :

<?php $var = 'test'; ?>
{{ $var }}
 
atau 
 
{{--*/ $var = 'test' /*--}}
{{ $var }}
 

 
  

No comments:

Post a Comment