Views and Blade Templates

πŸ“˜ Laravel πŸ‘ 63 views πŸ“… Dec 22, 2025
⏱ Estimated reading time: 2 min

Views in Laravel are responsible for presenting data to the user. They define the user interface (UI) of an application and are typically written using Blade, Laravel’s powerful templating engine. Blade makes it easy to create dynamic, reusable, and clean HTML layouts.


1. What Are Views?

Views contain the HTML that is sent to the browser. In Laravel, view files are stored in:

resources/views

View files use the .blade.php extension when using Blade syntax.


2. Creating a View

Example view file:

resources/views/welcome.blade.php
<!DOCTYPE html> <html> <head> <title>Welcome</title> </head> <body>

Welcome to Laravel

</body> </html>

To return a view from a route or controller:

return view('welcome');

3. Passing Data to Views

Data can be passed from controllers to views:

public function index() { $name = "Laravel"; return view('home', compact('name')); }

Using data in the view:

Welcome to {{ $name }}


4. Blade Syntax Basics

Blade uses simple and readable directives.

Displaying Data

{{ $variable }}

PHP Code

@php $count = 5; @endphp

5. Blade Control Structures

If Statements

@if($age >= 18)

Adult

@else

Minor

@endif

Loops

@foreach($users as $user)

{{ $user->name }}

@endforeach

6. Blade Layouts (Template Inheritance)

Blade allows reusable layouts.

Master Layout

resources/views/layouts/app.blade.php
<html> <body> @yield('content') </body> </html>

Child View

@extends('layouts.app') @section('content')

Home Page

@endsection

7. Blade Includes

Reusable components can be included:

@include('partials.header')

File location:

resources/views/partials/header.blade.php

8. Blade Components

Components help build reusable UI elements.

Example component:

php artisan make:component Alert

Usage:


9. Blade Comments

Blade comments are not visible in the browser:

{{-- This is a Blade comment --}}

10. Escaping HTML

Blade automatically escapes output for security.
To display raw HTML:

{!! $htmlContent !!}

Conclusion

Views and Blade templates in Laravel provide a clean and efficient way to build dynamic user interfaces. With features like layouts, components, and directives, Blade helps developers create maintainable and reusable frontend code while keeping logic separate from presentation.


πŸ”’ Some advanced sections are available for Registered Members
Register Now

Share this Post


← Back to Tutorials

Popular Competitive Exam Quizzes