Blade is Laravel's powerful templating engine that combines simplicity with flexibility. Unlike raw PHP templates, Blade provides elegant syntax while compiling to optimized PHP code. At ZIRA Software, Blade powers all our Laravel application views.
Template Inheritance
{{-- resources/views/layouts/app.blade.php --}}
<!DOCTYPE html>
<html>
<head>
<title>@yield('title') - My App</title>
@stack('styles')
</head>
<body>
<nav>
@include('partials.navigation')
</nav>
<main class="container">
@yield('content')
</main>
<footer>
@include('partials.footer')
</footer>
@stack('scripts')
</body>
</html>
{{-- resources/views/pages/home.blade.php --}}
@extends('layouts.app')
@section('title', 'Home')
@section('content')
<h1>Welcome to Our Application</h1>
@foreach($posts as $post)
<article>
<h2>{{ $post->title }}</h2>
<p>{{ $post->excerpt }}</p>
</article>
@endforeach
@endsection
@push('scripts')
<script src="/js/home.js"></script>
@endpush
Control Structures
{{-- Conditional Rendering --}}
@if($user->isAdmin())
<a href="/admin">Admin Panel</a>
@elseif($user->isModerator())
<a href="/moderate">Moderation Queue</a>
@else
<a href="/dashboard">Dashboard</a>
@endif
{{-- Unless (inverse if) --}}
@unless($user->subscribed())
<a href="/subscribe">Subscribe Now</a>
@endunless
{{-- Loops --}}
@forelse($notifications as $notification)
<div class="notification">
{{ $notification->message }}
</div>
@empty
<p>No notifications</p>
@endforelse
{{-- Loop variables --}}
@foreach($items as $item)
@if($loop->first)
<p>First item!</p>
@endif
<p>Item {{ $loop->iteration }} of {{ $loop->count }}</p>
@if($loop->last)
<p>Last item!</p>
@endif
@endforeach
Including Subviews
{{-- Basic include --}}
@include('partials.alert', ['type' => 'success', 'message' => 'Saved!'])
{{-- Include if exists --}}
@includeIf('partials.optional-sidebar')
{{-- Include when condition is true --}}
@includeWhen($user->hasPosts(), 'partials.posts-list', ['posts' => $user->posts])
{{-- Include first existing view --}}
@includeFirst(['custom.header', 'default.header'])
Custom Blade Directives
// app/Providers/AppServiceProvider.php
public function boot()
{
// Simple directive
Blade::directive('datetime', function ($expression) {
return "<?php echo ($expression)->format('M d, Y H:i'); ?>";
});
// Currency formatting
Blade::directive('money', function ($expression) {
return "<?php echo '$' . number_format($expression, 2); ?>";
});
// Authorization shortcut
Blade::directive('role', function ($role) {
return "<?php if(auth()->check() && auth()->user()->hasRole($role)): ?>";
});
Blade::directive('endrole', function () {
return "<?php endif; ?>";
});
}
{{-- Usage --}}
<p>Published: @datetime($post->created_at)</p>
<p>Price: @money($product->price)</p>
@role('admin')
<a href="/admin">Admin Area</a>
@endrole
Escaping and Raw Output
{{-- Escaped output (XSS safe) --}}
<p>{{ $userInput }}</p>
{{-- Raw unescaped output (use carefully!) --}}
<div>{!! $trustedHtml !!}</div>
{{-- Blade statement (not rendered) --}}
@{{ This will be rendered as-is for JavaScript frameworks }}
{{-- Verbatim block for JavaScript templates --}}
@verbatim
<div id="app">
{{ message }}
</div>
@endverbatim
Forms and CSRF
<form method="POST" action="/posts">
@csrf
@method('PUT')
<input type="text" name="title" value="{{ old('title', $post->title ?? '') }}">
@error('title')
<span class="error">{{ $message }}</span>
@enderror
<button type="submit">Save</button>
</form>
Conclusion
Blade templates provide the perfect balance between power and simplicity. Master template inheritance, custom directives, and components to build maintainable Laravel applications.
Need help with Laravel views? Contact ZIRA Software for development assistance.