- 1 year ago
- Zaid Bin Khalid
- 1,180 Views
-
2
Laravel Blade is a templating engine provided by the Laravel framework. It offers a convenient and expressive way to work with views and create dynamic, reusable components for your application’s user interface. Blade templates combine HTML markup with PHP code, allowing you to easily integrate logic and display dynamic data.
Here’s a detailed explanation of Laravel Blade templates with an example:
Blade Syntax
Blade templates use special syntax to separate PHP code from HTML markup. The most common Blade directives are:
{{ $variable }}
: Echoes the value of a variable.
@if
, @else
, @elseif
, @endif
: Conditionally execute code blocks.
@foreach
, @endforeach
: Iterate over an array or collection.
@for
, @endfor
: Perform a loop for a specified number of times.
@include('view')
: Include another Blade template.
Basic Usage
To create a Blade template, you can save it with the .blade.php
extension. For example, create a welcome.blade.php
file with the following content
<html>
<head>
<title>Welcome</title>
</head>
<body>
<h1>Welcome, {{ $name }}</h1>
@if ($isAdmin)
<p>You are an admin.</p>
@else
<p>You are a regular user.</p>
@endif
</body>
</html>
In this example, the {{ $name }}
syntax is used to display the value of the $name
variable. The @if
directive checks the value of the $isAdmin
variable and displays different messages based on the condition.
Passing Data to a View
To pass data to a Blade template, you can use the view()
function or the render()
method of a controller. For example, in a controller method:
public function welcome()
{
$name = 'John Doe';
$isAdmin = true;
return view('welcome', compact('name', 'isAdmin'));
}
In this example, the view()
function is used to load the welcome
template and pass the $name
and $isAdmin
variables to the view.
Blade Layouts
Blade templates allow you to define reusable layouts. You can create a layout file, such as layout.blade.php
, which contains the common structure and sections of your application. For example:
<html>
<head>
<title>@yield('title')</title>
</head>
<body>
<div class="container">
@yield('content')
</div>
</body>
</html>
In this example, the @yield
directive defines sections that will be filled in by the child views.
To extend a layout, use the @extends
directive in a child view:
@extends('layout')
@section('title', 'Welcome')
@section('content')
<h1>Welcome, {{ $name }}</h1>
<!-- Other content -->
@endsection
In this example, the @extends
directive specifies that the layout
template should be used as the base layout. The @section
directive defines the content that will be placed in the corresponding section of the layout.
Blade templates in Laravel provide a concise and powerful way to work with views and create dynamic user interfaces. The separation of PHP code and HTML markup makes the templates clean and easy to read. Blade’s syntax and directives enable you to perform conditional logic, loops, and include reusable components, making it a versatile tool for building expressive views in your Laravel applications.
- 1 year ago
- Zaid Bin Khalid
- 1,180 Views
-
2