- Instructiuni foreach(), forelse() si while()
- Obiectul $loop
- Includere Sub-View

Blade template contine functii pentru structuri de control comune cu PHP, cum sunt afirmatiile conditionale si instructiuni de parcurgere array.

Conditii if()

Puteti utiliza conditii if() folosind directivele: @if, @elseif, @else si @endif. Aceste directive functioneaza identic cu omologii lor PHP.
@if(count($records) === 1)
 I have one record.
@elseif(count($records) > 1)
 I have 1 records.
@else
 I don't have any records.
@endif

Verificare daca o sesiune exista

Pentru a verifica in template daca o anumita sesiune exista, folositi: @if(session('SESSION_KEY')).
@if(session('SESSION_KEY'))
  do something with session key
@else
  session key dosen't exist
@endif
- Sau: @if(Session::has('SESSION_KEY')).
@if(Session::has('SESSION_KEY'))
  do something with session key
@else
  session key dosen't exist
@endif

@unless si @endunless

@unless este inversul lui @if.
@unless ($condition) este la fel ca si PHP-ul: if(!$condition)
@unless(Auth::check())
 You are not signed in.
@endunless

@isset si @empty

Pe langa directivele conditionale, puteti folosi instructiunile @isset si @empty la fel ca functiile PHP respective:
@isset($records)
 $records is defined and is not null.
@endisset

@empty($records)
 $records is "empty".
@endempty

Instructiuni foreach(), forelse() si while()

Blade furnizeaza directive simple pentru a parcurge date din array si obiecte PHP: @for , @foreach , si @while. Functioneaza in Blade la fel ca in PHP:
@for($i=0; $i<8; $i++)
 Current value: 0<br>
@endfor

@foreach($users as $user)
 <p>This is user {{ $user->id }}</p>
@endforeach

@while($item = array_pop($items))
{{ $item->fun() }}<br>
@endwhile

@forelse

@forelse este un @foreach care permite sa aplici un callback daca obiectul specificat este gol.
@forelse($users as $user)
 <li>{{ $user->name }}</li>
@empty
 <p>No users</p>
@endforelse

@continue si @break

La fel ca in PHP, puteti sa sariti peste iteratia curenta (cu @continue) sau sa opriti parcurgerea (cu @break):
@foreach($users as $user)
  @if($user->type == 1)
    @continue
  @endif

 <li>{{ $user->name }}</li>

  @if($user->nr == 5)
    @break
  @endif
@endforeach
- Puteti sa includeti conditia direct in $continue si $break:
@foreach($users as $user)
  @continue($user->type == 1)

 <li>{{ $user->name }}</li>

  @break($user->nr == 5)
@endforeach

Obiectul $loop

Cand folositi foreach(), forelse() si while(), se creaza automat un obiect $loop in acea instructiune de parcurgere. Acest obiect contine proprietati utile:


- Exemplu:
@foreach($users as $user)
  @if($loop->first)
   This is the first iteration.
  @endif

  @if($loop->last)
   This is the last iteration.
  @endif

 <p>This is user {{ $user->id }}</p>
@endforeach
- Intr-o instructiune de parcurgere imbricata, puteti accesa obiectul $loop al instructiunii parinte prin proprietatea $loop->parent:
@foreach($users as $user)
  @foreach($user->posts as $post)
    @if($loop->parent->first)
     This is first iteration of the parent loop.
    @endif
  @endforeach
@endforeach

Includere Sub-Views

Directiva @include permite sa includeti in template un alt fisier view.
Variabilele din template pot fi folosite in view-ul inclus:
@include('sub_view')
Puteti sa transmiteti date suplimentare la view-ul inclus.
- Este indicat sa folositi directiva @includeIf, pentru a evita erori daca view-ul specificat nu exista.
@includeIf('sub_view', ['some'=>'data'])

@push cu @stack

Cu @push() si @stack() puteti defini bucati de template care pot fi refolosite in alt loc din acel view sau si in cele incluse.
//define push-stacks in scripts.blade.php

@push('script1')
<script src="/scripts_1.js"></script>
@endpush

@push('script2')
<script>
//other script
</script>
@endpush
Pentru a renda continutul unui @push, adaugati numele in directiva @stack():
@include('scripts')

@stack('script1')
Puteti folosi @include() si @stack() cu nume dinamic:
@includeIf('view_name'. $id)

@isset($js)
@stack('script'. $js)
@endisset

Exemplu practic

1. Copiati urmatorul cod si salvati-l intr-un fisier "resources/views/head.blade.php".
{{-- Define two js script push-stacks --}}
@push('script1')
<script>
alert('script_1');
</script>
@endpush

@push('script2')
<script>
alert('script_2');
</script>
@endpush

<!doctype html>
<html lang="{{app()->getLocale()}}">
<head>
<meta charset="utf-8">
<title>{{$title}}</title>
</head>
2. Copiati urmatorul cod si salvati-lintr-un fisier "resources/views/page.blade.php". Acesta include view-ul 'head' si foloseste un @stack javascript cu un nume variabil din $js.
@include('head')

<body>
<div class="content">
{!! $content !!}
</div>

@isset($js)
@stack('script'. $js)
@endisset
</body>
</html>
Am folosit afirmatia {!! $content !!} pentru a folosi cod HTML in $content.

3. Acum vom crea un controller pentru datele paginii.
Adaugati acest cod intr-un fisier "app/Http/Controllers/TestBlade.php".
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;

class TestBlade extends Controller {

  //receives the request object, and page ID; returns page view
  public function page(Request $request, $id=1){
    //define which javascript stak to use (set with @push in 'head' view)
    if($id ==1) $js =1;
    else if($id <4) $js =2;
    else $js ='';

    //set array with data to be passed to view() for template
    $data =[
      'title'=>'Title Page '. $id,
      'content'=>'<h3>Content of page'. $id .'</h3>',
      'js'=>$js
    ];

    return view('page', $data);
  }
}
4. Acum vom defini un Route pentru "/page{id?}", care foloseste controller-ul TestBlade.php.
Adaugati urmatorul cod in fisierul routes/web.php:
//calls page() method of TestBlade controller
Route::get('/page{id?}', 'TestBlade@page')->where('id', '[0-9]*');
5. Accesati in browser adresele URL urmatoare pentru a vedea rezultatul view-ului si alert-ul scriptului javascript din stack-ul folosit conform $id.
//localhost:8000/page

//localhost:8000/page3

//localhost:8000/page9

- Documentatie: Laravel - Blade Control Structures

Un Test simplu in fiecare zi

HTML
CSS
JavaScript
PHP-MySQL
Engleza
Spaniola
Care tag HTML5 defineste un text marcat, evidentiat?
<mark> <embed> <span>
<p>Cursuri graruite: <mark>MarPlo.net</mark> , jocuri, anime.</p>
Ce pseudo-clasa CSS defineste un stil la element cand mouse-ul e deasupra lui?
:focus :hover :active
a:hover {
  font-weight: bold;
  color: #00da01;
}
Clic pe functia ce returneaza un sir cu un numar rotunjit la x decimale.
toPrecision(x) toFixed(x) floor(x)
var num = 12.34567;
num = num.toFixed(2);
alert(num);       // 12.35
Indicati functia PHP care adauga continutul unui fisier intr-un array.
[) file() readfile()
$arr = file("a_file.txt", FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
var_export($arr);
Clic pe raspunsul potrivit la intrebarea: "What time is it?"
On the 7th of July 1996 It is a quarter to 5. Nice weather.
What time is it? It is a quarter to 5.
- Cat este ora? E 5 fara un sfert.
Indicati raspunsul potrivit la intrebarea: "¿Qué hora es?"
Hace buen tiempo. En el 7 de julio de 1996 Son las tres menos cuarto.
¿Qué hora es? Son las tres menos cuarto.
- Cat este ora? E 3 fara un sfert.
Laravel Blade Template - Controlul Structurii

Last accessed pages

  1. Scriere scripturi PHP elementare (1576)
  2. Conditional IF in Limba Engleza - Fraze Conditionale (20736)
  3. Curs PHP MySQL, Tutoriale si Scripturi PHP (6659)
  4. Espanol Chistes - Glume, Bancuri (877)
  5. Sintaxa JavaScript (2435)

Popular pages this month

  1. Gramatica limbii engleze - Prezentare Generala (2144)
  2. Prezentul simplu si continuu - Present Tense Simple and Continuous (1480)
  3. Cursuri si Tutoriale: Spaniola, Engleza, HTML, CSS, Php-Mysql, JavaScript, Ajax (1072)
  4. Trecutul simplu si continuu - Past Tense Simple and Continuous (1025)
  5. Prezent perfect si Prezent perfect continuu - Present perfect and Continuous (981)