10 Directive de Laravel Blade pe care probabil nu le stii

Stefanescu Mihai 2 years ago iNoob

Blade este template engine-ul din Laravel. Acest template engine iti usureaza mult viata de dezvoltator, dar majoritatea probabil nu ne folosim de foarte multe dintre feature-urile lui. In acest articol vreau sa fac o lista cu 10 dintre directivele pe foarte multi probabil nici nu le cunosc.

@forelse

In loc de count inainte de un foreach putem folos forelse astfel:

@if ($articles->count() > 0)
    @foreach($articles as $article)
        <a href="{{ route('article.show', $article->slug) }}">{{ $article->title }}</a>
    @endforeach
@else
    <p>no articles</p>
@endif  

Va deveni:

@forelse($articles as $article)
   <a href="{{ route('article.show', $article->slug) }}">{{ $article->title }}</a> 
@empty
    <p>no articles</p>
@endforelse

@each

In loc sa facem foreach si apoi sa includem un partial:

@foreach($articles as $article)
    @include('articles.details', ['article' => $article])
@endforeach

Putem face:

@each('articles.details', $articles, 'article')

@json

In loc de json_encode cand pasam o variabla in javascript

<script>
    var articles = {!! json_encode($articles) !!}
</script>

Putem face:

<script>
    var articles = @json($articles)
</script>

@verbatim

<div class="container">
    Salut, @{{ name }}
</div>

va deveni

@verbatim
    <div class="container">
        Salut, {{ name }}
    </div>
@endverbatim

@isset si @empty

De multe ori suntem pusi in situatia de a folosi isset() si empty(), dar blade vine in ajutorul nostru cu 2 diretive foarte simple

@isset($articles)
    //e acelasi lucru cu @if(isset($articles))
@endisset

la fel si pentru empty

@empty($articles)
    //e acelasi lucru cu @if(empty($articles))
@endempty

@php

Putem folosi aceasta directiva pentru a scrie php in blade

@php
    $value = $item[1] + $item[2];
@endphp

@push si @stack

Folosite pentru a adauga js-uri in stack

//asta sta in master view
<body>
    @stack('scripts')
</body>

// iar undeva intr-un child

@push('scripts')
    <script src="/item.js"></script>
@endpush

@inject

Ne ajuta la injectarea unui anumit serviciu.

@inject('metrics', 'App\Services\MetricsService')

<div>
    Total Vanzari: {{ $metrics->totalRevenue() }}
</div>

@includeWhen

In loc de a face un if sa vedem daca trebuie sa nu sa afisam un anumit partial putem folosi includeWhen

@if($userCanViewSection)
    @include('components.section');
@endif

poate fi inlocuit cu 

@includeWhen($userCanViewSection, 'components.section')

@hasSection

Verifica daca o sectiune este prezenta in subview

@hasSection('navigation')
    <div class="pull-right">
        @yield('navigation')
    </div>
    <div class="clearfix"></div>
@endif

Sper ca aceste directive de blade sa va fie folositoare! pentru orice sugestii puteti folosi sectiunea de comentarii de mai jos.

Programator de ~8 ani, am lucrat la proiecte din mai multe industrstrii, de la eCommerce la telecomunicatii la automatizari. In acest timp am folosi diferite tehnologii, de la PHP, MySQL, PostgreSql, RabbitMq, Redis, Memcached si altele.


Get in touch
Pentru nelamuriri, dubii, comentarii si chestii de pe suflet ne putem auzi pe Discord, Reddit sau poti deschide o discutie noua pe forum

women's health vitamin supplements # 6 months ago cheapest menopause relief options hormonal imbalance treatment natural hormone supplements online organic menopause supplements for energy
digestive health # 5 months ago Hi there, I check your blogs daily. Your humoristic style is witty, keep up the good work!
digestive health # 5 months ago Hi there, I check your blogs daily. Your humoristic style is witty, keep up the good work!
migraine meds that cause drowsiness # 5 months ago What's up to all, how is everything, I think every one is getting more from this site, and your views are pleasant designed for new people.
Puedes comprar furosemide 100 mg sin receta en Bélgica # 1 month ago Wow that was unusual. I just wrote an really long comment but after I clicked submit my comment didn't show up. Grrrr... well I'm not writing all that over again. Anyway, just wanted to say superb blog!
venta de vancenase en línea en España # 1 month ago Wow that was odd. I just wrote an extremely long comment but after I clicked submit my comment didn't appear. Grrrr... well I'm not writing all that over again. Regardless, just wanted to say great blog!
clarithromycin pedido en Buenos Aires # 1 month ago I’m not that much of a internet reader to be honest but your sites really nice, keep it up! I'll go ahead and bookmark your website to come back down the road. Many thanks
achat de sildenafil 100 mg en Italie # 1 month ago Hi everyone, it's my first pay a visit at this site, and piece of writing is truly fruitful in support of me, keep up posting these types of posts.
koop sinequan 75 mg online in Nederland # 1 month ago Hello just wanted to give you a quick heads up and let you know a few of the images aren't loading correctly. I'm not sure why but I think its a linking issue. I've tried it in two different web browsers and both show the same outcome.
avapro disponible sans ordonnance à Paris # 1 month ago With havin so much content and articles do you ever run into any problems of plagorism or copyright violation? My blog has a lot of unique content I've either created myself or outsourced but it appears a lot of it is popping it up all over the web without my agreement. Do you know any ways to help reduce content from being ripped off? I'd definitely appreciate it.
clomicalm en ligne sans ordonnance en France # 1 month ago Genuinely when someone doesn't be aware of afterward its up to other visitors that they will assist, so here it happens.
aankoop van glucobete in Utrecht online # 1 month ago I've read several good stuff here. Definitely value bookmarking for revisiting. I surprise how much effort you set to make this type of wonderful informative website.
commander du montelukast 10 mg en France # 1 month ago whoah this blog is fantastic i really like studying your articles. Stay up the great work! You realize, a lot of persons are looking around for this information, you could help them greatly.
Louvenia # 4 weeks ago It's an awesome piece of writing in support of all the online people; they will get benefit from it I am sure.
goldenpaisa # 2 weeks ago <a href=https://www.sapporovn.com/maxx-crosby-white-jersey-nflat>maxx crosby white jersey</a><a href=https://www.sapporovn.com/make-your-own-nba-jersey-online-nflat>make your own nba jersey online</a><a href=https://www.sapporovn.com/womens-sixers-apparel-nflat>womens sixers apparel</a><a href=https://www.sapporovn.com/miami-heats-new-uniforms-nflat>miami heats new uniforms</a> <a href="https://www.tiopepi.net/boston-red-sox-ladies-hats-zillow-handt">boston red sox ladies hats zillow</a> <a href="https://www.execedits.net/ccm-vintage-jerseys-for-cheap-jerseyi">ccm vintage jerseys for cheap</a> <a href="https://www.felicatech.com/pittsburgh-pirates-hat-meaning-xbox-one-capo">pittsburgh pirates hat meaning xbox one</a><a href="https://www.felicatech.com/michael-jordan-fitted-hats-90s-capo">michael jordan fitted hats 90s</a><a href="https://www.felicatech.com/boston-red-sox-dog-hat-2017-capo">boston red sox dog hat 2017</a><a href="https://www.felicatech.com/hats-dolphin-mall-nj-capo">hats dolphin mall nj</a> <a href="https://www.smtlrtcc.net/air-force-1-s-black-shoespt">air force 1 s black</a> <a href=https://www.thestmc.com/jordan-6-low-white-rot-skoie>jordan 6 low white rot</a><a href=https://www.thestmc.com/jordan-13-og-white-black-skoie>jordan 13 og white black</a><a href=https://www.thestmc.com/nike-air-more-uptempo-bambini-skoie>nike air more uptempo bambini</a><a href=https://www.thestmc.com/adidas-nmd-ri-maschio-skoie>adidas nmd ri maschio</a> [url=http://www.goldenpaisa.net/]goldenpaisa[/url]
Hollie # 1 week ago Thanks for sharing your thoughts about laravel blade. Regards
Club-ul este dedicat membrilor si ofera access la mai multe zone ale website-ului.
Login Register

🔖 Bookmarks
✨ Pentru a sustine aceasta comunitate am sa te rog sa te autentifici sau sa te inregistrezi!

🌪️ Discord
Back to top
Folosim cookie-uri pentru a oferi functionalitatile critice ale aplicatiei Invata-Programare. Folosim cookie-uri si pentru a analiza traficul, pentru care e nevoie de consimtamantul dvs. explicit.