Blade Templates
6 min read Quiz at the end
Build dynamic HTML with Blade directives, layouts, @section, @yield, components, and @auth.
Blade Templating
{{-- resources/views/layouts/app.blade.php --}}
<!DOCTYPE html>
<html>
<body>
@yield("content")
@stack("scripts")
</body>
</html>
{{-- resources/views/posts/index.blade.php --}}
@extends("layouts.app")
@section("content")
@foreach ($posts as $post)
<h2>{{ $post->title }}</h2> {{-- escaped output --}}
{!! $post->html_body !!} {{-- unescaped --}}
@endforeach
{{ $posts->links() }} {{-- pagination --}}
@endsection
@push("scripts")
<script src="/app.js"></script>
@endpush
{{-- Directives --}}
@if (auth()->check())
@elseif ($user->isAdmin())
@else
@endif
@foreach ($items as $item)
@endforeach
@forelse ($posts as $post)
@empty
<p>No posts.</p>
@endforelse
@auth @endauth
@guest @endguest
@can("edit-post", $post) @endcan
Topic Quiz · 4 questions
Test your understanding before moving on
1. How do you safely output a variable in Blade?
💡 {{ $var }} outputs escaped HTML — safe against XSS attacks.
2. When should you use {!! $var !!}?
💡 {!! $var !!} outputs raw unescaped HTML — dangerous with user input.
3. Which Blade directive checks auth?
💡 @auth renders content only for authenticated users.
4. What does @yield("content") do?
💡 @yield marks where the child view @section will be inserted.