Laravel Blade Templates: An Introductory Guide for Beginners

NatureNature
3 min read

🧩 What is Laravel Blade?

Imagine you're building a website, and you need to display dynamic content—like showing a user's name, listing products, or displaying messages. Laravel Blade is a templating engine that helps you write clean and efficient HTML mixed with PHP code to achieve this.

In simple terms, Blade allows you to create HTML templates that can dynamically display data and handle logic, making your code more readable and maintainable.


🖥️ Basic Blade Syntax

1. Displaying Data

To display data in a Blade template, you use double curly braces:

<p>Hello, {{ $name }}!</p>

This will output:

Hello, John!

Blade automatically escapes the data to prevent security vulnerabilities like XSS attacks.


2. Raw Output

If you need to display raw HTML (be cautious with this), you can use {!! !!}:

<p>{!! $htmlContent !!}</p>

This will render the HTML content as by parsing the HTML.


3. Conditionals

Blade makes it easy to write conditional statements:

@if($age >= 18)
    <p>You are an adult.</p>
@else
    <p>You are a minor.</p>
@endif

This checks if the $age is 18 or more and displays the appropriate message.


4. Loops

To iterate over data, Blade provides several loop structures:

  • For Loop

      @for($i = 0; $i < 5; $i++)
          <p>Iteration {{ $i }}</p>
      @endfor
    
  • Foreach Loop

      @foreach($users as $user)
          <p>{{ $user->name }}</p>
      @endforeach
    
  • While Loop

      @while($count < 5)
          <p>Count is {{ $count }}</p>
          @php $count++; @endphp
      @endwhile
    

These loops help you display lists of items or repeat content dynamically.


🧱 Blade Layouts and Sections

To avoid repeating the same HTML structure (like headers, footers, etc.) in every view, Blade allows you to define a master layout and extend it in other views.

1. Defining a Layout

Create a layout file, e.g., resources/views/layouts/app.blade.php:

<!DOCTYPE html>
<html>
<head>
    <title>@yield('title')</title>
</head>
<body>
    <div class="container">
        @yield('content')
    </div>
</body>
</html>

Here, @yield('title') and @yield('content') are placeholders for dynamic content.


2. Extending a Layout

In your view file, e.g., resources/views/home.blade.php:

bladeCopyEdit@extends('layouts.app')

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

@section('content')
    <h1>Welcome to the Home Page</h1>
    <p>This is the content of the home page.</p>
@endsection

This structure allows you to maintain a consistent layout across your application while defining unique content for each page.


🔗 Including Other Views

You can include other Blade views within a view using the @include directive:

@include('partials.header')

This is useful for including common elements like headers or footers.


🧪 Example: Product Listing Page

Let's create a simple product listing page using Blade.

1. Route Definition

In your routes/web.php:

Route::get('/products', function () {
    $products = [
        ['name' => 'Product 1', 'price' => 100],
        ['name' => 'Product 2', 'price' => 200],
        ['name' => 'Product 3', 'price' => 300],
    ];
    return view('products.index', compact('products'));
});

2. Blade View

Create a Blade view resources/views/products/index.blade.php:

@extends('layouts.app')

@section('title', 'Product List')

@section('content')
    <h1>Product List</h1>
    <ul>
        @foreach($products as $product)
            <li>{{ $product['name'] }} - ${{ $product['price'] }}</li>
        @endforeach
    </ul>
@endsection

This will display a list of products with their names and prices.



✅ Conclusion

Laravel Blade simplifies the process of building dynamic and maintainable web applications. By using Blade's features like data display, control structures, layouts, and components, you can create clean and efficient templates. Whether you're building a simple website or a complex application, Blade provides the tools you need to manage your views effectively.

0
Subscribe to my newsletter

Read articles from Nature directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Nature
Nature