0
0
Laravelframework~5 mins

Blade template syntax in Laravel

Choose your learning style9 modes available
Introduction

Blade helps you write HTML mixed with PHP easily. It makes your web pages cleaner and simpler to manage.

When you want to create dynamic web pages in Laravel.
When you need to show data from your database inside HTML.
When you want to reuse parts of your web page like headers or footers.
When you want to control what shows on the page based on conditions.
When you want to loop over lists of items to display them.
Syntax
Laravel
@directive(parameters)
{{ expression }}
{{-- comment --}}
@php
  // PHP code here
@endphp
Use double curly braces {{ }} to show variables safely (it escapes HTML).
Directives start with @ and control logic like loops and conditions.
Examples
Shows the value of the variable $name safely inside HTML.
Laravel
{{ $name }}
Shows text only if the condition is true.
Laravel
@if($age >= 18)
  You are an adult.
@endif
Loops over a list of users and shows each user's name.
Laravel
@foreach($users as $user)
  <p>{{ $user->name }}</p>
@endforeach
Includes another Blade file called header.blade.php here.
Laravel
@include('header')
Sample Program

This Blade template shows a welcome message with the user's name. It checks if the user is an adult or minor and shows a message. Then it lists user names in a bullet list.

Laravel
@php
  $name = 'Alice';
  $age = 20;
  $users = [
    (object)['name' => 'Bob'],
    (object)['name' => 'Carol']
  ];
@endphp

<h1>Welcome, {{ $name }}!</h1>

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

<h2>User List:</h2>
<ul>
@foreach($users as $user)
  <li>{{ $user->name }}</li>
@endforeach
</ul>
OutputSuccess
Important Notes

Always use {{ }} to print variables to avoid security risks from raw HTML.

Blade directives like @if, @foreach make your templates easy to read and write.

You can write plain PHP inside @php ... @endphp blocks if needed.

Summary

Blade syntax mixes PHP and HTML simply and safely.

Use {{ }} to show variables and @directives for logic.

Blade helps keep your web pages clean and easy to maintain.