Cheat Sheet
// Defining A Layout
@yield('name')
@section('name')
@endsection/@show
// Extending A Layout
@extends('template')
@section('name') ... content ... @endsection
@section('name', 'value')
@section('name', $value)
// Displaying
{{$name}} // escaped
{{$name or 'Default'}} // if not set
{!! $name !!} // unescaped
@{{ name }} // ignore braces - output as is
@verbatim // ignore all braces in this block @endverbatim
// If Statements
@if, @elseif, @else and @endif
@unless, @endunless
// Loops
@for...@endfor
@foreach...@endforeach
@forelse...@empty...@endforelse
@while...@endwhile
@continue, @continue($user->type==1)
@break, $break($user->type==1)
$loop - index, iteration, remaining, count, first, last, depth, parent
// Comments
{{-- This comment will not be present in the rendered HTML --}}
// PHP
@php...@endphp
// Including sub-views
@include('template')
@include('template', ['some' => 'data'])
@includeIf('not_exist', ['some' => 'data']) // won't an error
@each('jobtemplate', $jobs, 'job', 'emptyresult')
// Stacks
@push('name')...@endpush //
@stack('name')
// Service injection
@inject('metrics', 'App\Services\MetricsService')
{{ @metrics->monthlyRevenue() }}
// Extending
Blade::directive('datetime', function ($expression) {
return "<?php echo ($expression)->format('m/d/Y H:i'); ?>";
});
Better security
You may find useful directives to fight XSS here - laravelgems/blade-escape
Example:
<style>
.userPrefix:before { content: "@css($content)"; }
</style>
<div>
<label class="userPrefix">@text($label)</label>
<input type="text" name="custom" value="@attr($value)"/>
</div>
<a href="/profile?u=@param($username)">Profile</a>
<button onclick="callMyFunction('@js($username)');">Validate</button>
<script>
var username = "@js($username)";
</script>
Testing page available too - http://laragems.com/package/blade-escape/test