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.
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.
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.
Let’s move our "Task List" away from the controller and into a proper view.
resources/views.tasks.blade.php.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:
PHPpublic function index() { return view('tasks'); }
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:
PHPpublic 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.
HTMLstyle="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.
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:
HTMLstyle="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.
TasksController and add a new variable $pageTitle = 'My Awesome Task List';.view function using the compact('pageTitle') helper.tasks.blade.php, display $pageTitle inside an <h1> tag.$pageTitle is long (use strlen()), display a special sub-header..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.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.
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 moreLearn to customize Laravel validation error messages to provide clear, helpful feedback to your users. Master field-specific errors and language files.
Introduction to Blade Templating
Protecting Routes with Middleware
Understanding CSRF Protection
Preventing Mass Assignment
Task Manager: Securing the Application
Introduction to Route Model Binding
Updating Existing Records
Deleting Records
Using Named Routes
Task Manager: Completing CRUD Functionality
Introduction to Database Relationships
Querying Related Data
Handling File Uploads
Using Flash Messages for User Feedback
Task Manager: Adding Status and Priorities
Introduction to Artisan Commands
Debugging with Laravel Tinker
Understanding Service Providers
Using View Composers
Task Manager: Refactoring for Clean Code
Introduction to Testing
Testing Forms and Validation
Using Database Transactions
Handling Global Exceptions
Preparing for Production
Environment Security Best Practices
Managing Assets in Production
Task Manager: Deployment Preparation