Skip to content

Instantly share code, notes, and snippets.

@CiprianSpiridon
Last active November 13, 2024 19:32
Show Gist options
  • Save CiprianSpiridon/f4d7fe0d8a51f0714b62 to your computer and use it in GitHub Desktop.
Save CiprianSpiridon/f4d7fe0d8a51f0714b62 to your computer and use it in GitHub Desktop.
Laravel Blade Template Cheatsheet
{{ $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
@Isset($records)
// $records is defined and is not null...
@endisset
@production
//code to be displayed just when .env is set to production
@endproduction
@auth
//code for logged-in users
@endauth
@guest
//code for guest users
@endguest
@uF4No
Copy link

uF4No commented Sep 1, 2017

Great mate! This is just what I was looking for!
Thanks :)

@technoknol
Copy link

@guest missing.

@dinukanadeeshan
Copy link

What is the meaning of '{!! some code !!} '

@oliveris
Copy link

{{ $var }} will echo the content but gets sent through htmlspecialchars function to prevent XSS attacks.
{!! $var !!} will echo the content and DOES NOT get sent through htmlspecialchars function.

@venkataadithan
Copy link

@Isset($records)
// $records is defined and is not null...
@endisset

//isset is missing in this cheat sheet

@osomaher2020
Copy link

What is the meaning of '{!! some code !!} '

means skip the special characters which like a code

@jtrod
Copy link

jtrod commented Apr 4, 2022

@production
//code to be displayed just when .env is set to production
@endproduction

@scarlight
Copy link

@error is missing

@smylmrz
Copy link

smylmrz commented Dec 5, 2022

Might be useful
@auth
code for logged-in users
@endauth

@guest
code for guest users
@endguest

@CiprianSpiridon
Copy link
Author

Updated the gist with your suggestions as this was created in 2014 and never touched since

@di-void
Copy link

di-void commented Dec 30, 2022

Thanks Chief

@MukhtaarAziz
Copy link

there is more like:
@stack('stack-name')
@Push
content pushed in the 'stack-name'
@endpush

@artulloss
Copy link

You should add

@php
    //php code
@endphp

@amine-bourouba
Copy link

You should add

@php
    //php code
@endphp

Isn't the whole purpose of using templating engines is to avoid using PHP in HTML?

@amine-bourouba
Copy link

@csrf so that the CSRF protection middleware can validate the request.

@gdevlugt
Copy link

gdevlugt commented Jun 7, 2024

To check if the authenticated user is authorized to perform an ability set using a gate or a policy.

@can(ability, model instance) 
// ...
@endcan

@cannot(ability, model instance) 
// ...
@endcannot

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment