Blade is Laravel's built-in templating engine that simplifies the process of creating dynamic views by providing a clean and expressive syntax. It allows you to embed PHP code directly into your HTML while maintaining readability and maintainability.
Blade was introduced as a part of Laravel to provide an elegant and developer-friendly alternative to traditional PHP templating.
@if
and @foreach
, make conditional rendering and loops more readable.@auth
, @guest
, and @csrf
to streamline your templates.Define a layout in resources/views/layouts/app.blade.php
:
<!DOCTYPE html>
<html>
<head>
<title>@yield('title')</title>
</head>
<body>
<div class="container">
@yield('content')
</div>
</body>
</html>
Then extend it in a child view:
@extends('layouts.app')
@section('title', 'Welcome Page')
@section('content')
<h1>Welcome to Laravel!</h1>
@endsection
This approach ensures clean and maintainable views.