0
0
LaravelHow-ToBeginner · 3 min read

How to Create a Blade Template in Laravel Quickly

To create a Blade template in Laravel, create a .blade.php file inside the resources/views directory. Use Blade syntax like @extends, @section, and @yield to structure your template and include dynamic content.
📐

Syntax

A Blade template file uses the .blade.php extension and is stored in the resources/views folder. Common Blade directives include:

  • @extends('layout'): Inherit a layout template.
  • @section('name') and @endsection: Define content sections.
  • @yield('name'): Display a section's content in a layout.
  • {{ $variable }}: Echo variables safely.
php
<!-- resources/views/example.blade.php -->
@extends('layout')

@section('title')
  Page Title
@endsection

@section('content')
  <p>Hello, {{ $name }}!</p>
@endsection
💻

Example

This example shows a simple Blade template extending a layout and displaying a variable passed from a controller.

php
<!-- resources/views/layout.blade.php -->
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>@yield('title')</title>
</head>
<body>
  <header><h1>My Website</h1></header>
  <main>
    @yield('content')
  </main>
  <footer>© 2024</footer>
</body>
</html>


<!-- resources/views/welcome.blade.php -->
@extends('layout')

@section('title', 'Welcome Page')

@section('content')
  <p>Welcome, {{ $name }}!</p>
@endsection
Output
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Welcome Page</title> </head> <body> <header><h1>My Website</h1></header> <main> <p>Welcome, John!</p> </main> <footer>© 2024</footer> </body> </html>
⚠️

Common Pitfalls

  • Forgetting to use the .blade.php extension causes Laravel to treat the file as plain PHP.
  • Not placing templates inside resources/views means Laravel cannot find them.
  • Using @section without a matching @yield in the layout results in content not showing.
  • Echoing variables without {{ }} or escaping can cause errors or security issues.
php
<!-- Wrong: missing .blade.php extension -->
<!-- resources/views/welcome.php -->
@extends('layout')
@section('content')
  <p>Hello!</p>
@endsection


<!-- Right: correct extension -->
<!-- resources/views/welcome.blade.php -->
@extends('layout')
@section('content')
  <p>Hello!</p>
@endsection
📊

Quick Reference

DirectivePurposeExample
@extends('layout')Inherit a layout template@extends('layout')
@section('name') ... @endsectionDefine a content section@section('content') ... @endsection
@yield('name')Display a section's content@yield('title')
{{ $variable }}Echo a variable safely

Hello, {{ $name }}!

@include('view')Include another Blade view@include('header')

Key Takeaways

Create Blade templates as .blade.php files inside resources/views.
Use @extends and @section to build layouts and content areas.
Always echo variables with {{ }} to escape output safely.
Ensure your layout uses @yield to display sections from child templates.
Keep Blade files organized for easy maintenance and readability.