agyanadda@gmail.com

Laravel Blade Templates

Laravel Basics 1 min read Updated Dec 2, 2026 Topic 6 of 13

Blade is Laravel's template engine. Blade files end with .blade.php and live in resources/views.

Showing data

  • {{ $name }} — prints a value escaped, which protects against XSS attacks
  • {!! $html !!} — prints raw HTML; use only for content you trust

Conditions and loops

  • @if, @elseif, @else, @endif
  • @foreach ($posts as $post) ... @endforeach
  • @forelse ... @empty ... @endforelse — handles an empty list

Layouts

Write the page frame once in layouts/app.blade.php with @yield('content'). Each page uses @extends('layouts.app') and fills @section('content').

Other useful directives

  • @include('partials.nav') — insert another view
  • @csrf — adds the security token every POST form needs
  • @auth ... @endauth — show content only to logged-in users

Example

Example — Layout and child page PHP
{{-- resources/views/layouts/app.blade.php --}}
<html>
<body>
  <h1>My Blog</h1>
  @yield('content')
</body>
</html>

{{-- resources/views/posts/index.blade.php --}}
@extends('layouts.app')

@section('content')
  <ul>
    @forelse ($posts as $post)
      <li>{{ $post }}</li>
    @empty
      <li>No posts yet.</li>
    @endforelse
  </ul>
@endsection

The page shows the "My Blog" heading from the layout, then a list of posts — or "No posts yet." if the array is empty.

Test Yourself With Exercises

Exercise 1 Multiple Choice

Which Blade syntax prints a value safely escaped?

Exercise 2 True / False

@forelse lets you show a message when the list is empty.

Exercise 3 Fill in the Blank

A child view uses @___('layouts.app') to use a layout.

Frequently Asked Questions

Only for HTML you fully trust, such as content written by an admin and cleaned before saving. Never use it for user input, because it skips escaping and can allow XSS attacks.