Mahamudul Hasan Rubel
HomeAboutProjectsSkillsExperienceBlogCoursesPhotosContact
Mahamudul Hasan Rubel

Senior Software Engineer crafting high-performance web applications and SaaS platforms.

Navigation

  • Home
  • About
  • Projects
  • Skills
  • Experience
  • Blog
  • Courses
  • Photos
  • Contact

Get in Touch

Available for senior/lead roles and consulting.

bd.mhrubel@gmail.comHire Me

© 2026 Mahamudul Hasan Rubel. All rights reserved.

Built with using Next.js 16 & Tailwind v4

Back to Blog
Lesson 11 of the Laravel Fundamentals: From Zero to Your First App course
LaravelJune 25, 20263 min read

Introduction to Blade: Mastering Laravel's Templating Engine

Learn how to use Blade to render dynamic data in Laravel. Discover how to create .blade.php files, use echo syntax, and implement essential directives.

LaravelBladePHPTemplatingWeb Developmentbackend

Previously in this course, we covered Task Manager: Implementing the Task List Route, where we returned a simple string response from a controller. In this lesson, we'll replace that hardcoded string with a real view, moving from basic routing into the world of Blade, Laravel's native templating engine.

What is Blade?

Blade is a powerful, elegant templating engine that allows you to write plain PHP in your views without the messy syntax of traditional PHP tags. Instead of writing <?php echo $name; ?>, you write {{ $name }}.

Blade files are saved with the .blade.php extension and are stored in the resources/views directory. When a user requests a page, Laravel compiles these files into plain, cached PHP code, meaning there is zero performance overhead for using the engine.

Creating Your First Blade View

Let’s move our "Task List" away from the controller and into a proper view.

  1. Navigate to resources/views.
  2. Create a new file named tasks.blade.php.
  3. Add the following HTML structure:
HTML
<!DOCTYPE html>
style="color:#808080"><style="color:#4EC9B0">html>
style="color:#808080"><style="color:#4EC9B0">head>
    style="color:#808080"><style="color:#4EC9B0">title>My Task Liststyle="color:#808080"></style="color:#4EC9B0">title>
style="color:#808080"></style="color:#4EC9B0">head>
style="color:#808080"><style="color:#4EC9B0">body>
    style="color:#808080"><style="color:#4EC9B0">h1>Tasksstyle="color:#808080"></style="color:#4EC9B0">h1>
style="color:#808080"></style="color:#4EC9B0">body>
style="color:#808080"></style="color:#4EC9B0">html>

To display this in your browser, update your TasksController to return the view helper instead of a string:

PHP
public function index()
{
    return view('tasks');
}

Outputting Data with Echo Syntax

The most common task in templating is displaying data passed from the controller. Blade uses "curly brace" syntax to safely output variables.

Update your controller to pass an array of data to the view:

PHP
public function index()
{
    $tasks = ['Buy milk', 'Clean the house', 'Finish Laravel lesson'];
    
    return view('tasks', ['tasks' => $tasks]);
}

Now, update tasks.blade.php to access this data. Blade automatically runs htmlspecialchars() on your output to prevent XSS attacks, making your app secure by default.

HTML
style="color:#808080"><style="color:#4EC9B0">h1>Tasksstyle="color:#808080"></style="color:#4EC9B0">h1>
style="color:#808080"><style="color:#4EC9B0">ul>
    style="color:#808080"><style="color:#4EC9B0">li>{{ $tasks[0] }}style="color:#808080"></style="color:#4EC9B0">li>
    style="color:#808080"><style="color:#4EC9B0">li>{{ $tasks[1] }}style="color:#808080"></style="color:#4EC9B0">li>
style="color:#808080"></style="color:#4EC9B0">ul>

If you ever need to output raw HTML (be careful!), you can use {!! $variable !!}. Only use this for trusted content, like data coming directly from your own database.

Using Basic Blade Directives

Directives are the "power tools" of Blade. They look like functions prefixed with an @ symbol and handle common programming tasks like loops and conditionals.

Let’s improve our task list to handle empty states and loop through our array dynamically:

HTML
style="color:#808080"><style="color:#4EC9B0">h1>Tasksstyle="color:#808080"></style="color:#4EC9B0">h1>

@if(count($tasks) > 0)
    style="color:#808080"><style="color:#4EC9B0">ul>
        @foreach($tasks as $task)
            style="color:#808080"><style="color:#4EC9B0">li>{{ $task }}style="color:#808080"></style="color:#4EC9B0">li>
        @endforeach
    style="color:#808080"></style="color:#4EC9B0">ul>
@else
    style="color:#808080"><style="color:#4EC9B0">p>No tasks found!style="color:#808080"></style="color:#4EC9B0">p>
@endif
  • @if: Starts a conditional block.
  • @foreach: Iterates over the collection.
  • @else: Provides a fallback.
  • @endif: Closes the block.

These directives keep your templates readable. While you could write native PHP inside your views, sticking to Blade directives is a best practice for clean, maintainable code.

Hands-on Exercise

  1. Open your TasksController and add a new variable $pageTitle = 'My Awesome Task List';.
  2. Pass this variable to the view function using the compact('pageTitle') helper.
  3. In tasks.blade.php, display $pageTitle inside an <h1> tag.
  4. Add a check: if the $pageTitle is long (use strlen()), display a special sub-header.

Common Pitfalls

  • Forgetting the extension: Always name your files .blade.php. If you name it just .php, Laravel won't compile the Blade directives, and you'll see raw @if syntax in your browser.
  • Case Sensitivity: View names are case-sensitive on most Linux servers (though not on macOS/Windows). Always stick to lowercase filenames to avoid "View not found" errors when you deploy.
  • Over-logic: Keep your views thin. If you find yourself writing complex database queries or heavy calculations inside a Blade template, move that logic into your controller or an Eloquent model.

Recap

We've moved from hardcoded strings to dynamic Blade templates. By using the {{ }} syntax for output and directives like @foreach and @if, you can create complex, data-driven interfaces. As you continue, you'll find that mastering these views is the key to building a robust UI.

Up next: We will learn how to use Laravel Blade Templates: Mastering Inheritance and Sections to avoid repeating our HTML code across different pages.

Previous lessonTask Manager: Implementing the Task List RouteNext lesson Using Blade Layouts and Sections
Back to Blog

Similar Posts

LaravelJune 25, 20263 min read

Mastering Blade Directives for Loops and Conditionals

Learn how to use Blade directives like @if, @foreach, and @forelse to control your view logic and render dynamic lists in your Laravel applications efficiently.

Read more
LaravelJune 25, 20263 min read

Customizing Validation Error Messages for Better Laravel UX

Learn to customize Laravel validation error messages to provide clear, helpful feedback to your users. Master field-specific errors and language files.

Part of the course

Laravel Fundamentals: From Zero to Your First App

beginner · Lesson 11 of 52

  1. 1

    Setting Up the Local Development Environment

    4 min
  2. 2

    Installing Laravel and Exploring Directory Structure

    3 min
  3. 3

    Understanding the .env File and Configuration

    3 min
Read more
LaravelJune 25, 20264 min read

Implementing Blade Partials: A Guide to DRY Laravel Views

Stop repeating yourself in Blade templates. Learn how to implement Blade partials to extract common UI elements and keep your Laravel views maintainable.

Read more
  • 4

    The Laravel Application Lifecycle

    4 min
  • 5

    Initializing the Task Manager Project

    3 min
  • 6

    Defining Basic Web Routes

    4 min
  • 7

    Using Route Parameters

    3 min
  • 8

    Creating Your First Controller

    3 min
  • 9

    Returning Responses and Redirects

    3 min
  • 10

    Task Manager: Implementing the Task List Route

    3 min
  • 11

    Introduction to Blade Templating

    3 min
  • 12

    Using Blade Layouts and Sections

    3 min
  • 13

    Implementing Blade Partials

    4 min
  • 14

    Mastering Blade Directives for Loops and Conditionals

    3 min
  • 15

    Task Manager: Building the User Interface

    3 min
  • 16

    Understanding Database Migrations

    3 min
  • 17

    Working with Eloquent Models

    3 min
  • 18

    Performing Basic CRUD Operations

    3 min
  • 19

    Seeding the Database

    3 min
  • 20

    Task Manager: Displaying Real Database Records

    3 min
  • 21

    Capturing User Input from Forms

    4 min
  • 22

    Introduction to Laravel Validation

    3 min
  • 23

    Customizing Validation Error Messages

    3 min
  • 24

    Using Form Requests for Validation

    3 min
  • 25

    Introduction to Authentication

    4 min
  • 26

    Protecting Routes with Middleware

    Coming soon
  • 27

    Understanding CSRF Protection

    Coming soon
  • 28

    Preventing Mass Assignment

    Coming soon
  • 29

    Task Manager: Securing the Application

    Coming soon
  • 30

    Introduction to Route Model Binding

    Coming soon
  • 31

    Updating Existing Records

    Coming soon
  • 32

    Deleting Records

    Coming soon
  • 33

    Using Named Routes

    Coming soon
  • 34

    Task Manager: Completing CRUD Functionality

    Coming soon
  • 35

    Introduction to Database Relationships

    Coming soon
  • 36

    Querying Related Data

    Coming soon
  • 37

    Handling File Uploads

    Coming soon
  • 38

    Using Flash Messages for User Feedback

    Coming soon
  • 39

    Task Manager: Adding Status and Priorities

    Coming soon
  • 40

    Introduction to Artisan Commands

    Coming soon
  • 41

    Debugging with Laravel Tinker

    Coming soon
  • 42

    Understanding Service Providers

    Coming soon
  • 43

    Using View Composers

    Coming soon
  • 44

    Task Manager: Refactoring for Clean Code

    Coming soon
  • 45

    Introduction to Testing

    Coming soon
  • 46

    Testing Forms and Validation

    Coming soon
  • 47

    Using Database Transactions

    Coming soon
  • 48

    Handling Global Exceptions

    Coming soon
  • 49

    Preparing for Production

    Coming soon
  • 50

    Environment Security Best Practices

    Coming soon
  • 51

    Managing Assets in Production

    Coming soon
  • 52

    Task Manager: Deployment Preparation

    Coming soon
  • View full course