Mastering Laravel Blade Templates

Laravel Blade Templates Example

Introduction:

Laravel Blade is a powerful templating engine that simplifies the process of creating dynamic views in Laravel applications. In this tutorial, we’ll cover a wide range of Blade features, from the basics to more advanced techniques, empowering you to leverage Blade effectively in your projects.

Getting Started with Blade Templates:

Blade templates in Laravel are stored in the resources/views directory. You can create a new Blade file with the .blade.php extension to start using it. Let’s create a simple Blade template called welcome.blade.php :

<!DOCTYPE html>
<html>
<head>
    <title>Welcome</title>
</head>
<body>
    <h1>Hello, {{ $name }}!</h1>
</body>
</html>

Basic Blade Directives:

Blade provides several directives to help you work with templates more efficiently. Some of the basic directives include:

  •  {{ $variable }} : Echoing variables.
  •  @if, @elseif, @else, @endif : Conditional statements.
  •  @foreach, @endforeach: Looping through arrays.
  •  @for , @endfor: Looping with a specified number of iterations.

Blade Layouts and Extending:

Blade allows you to create reusable layouts and extend them in other views. Let’s create a layout file called layout.blade.php:

<!DOCTYPE html>
<html>
<head>
    <title>@yield('title')</title>
</head>
<body>
    <div class="container">
        @yield('content')
    </div>
</body>
</html>

You can extend this layout in other views using the @extends directive:

@extends('layout')

@section('title', 'Home Page')

@section('content')
    <h1>Welcome to our website!</h1>
@endsection

Blade Components and Slots:

Blade components allow you to create reusable UI components. Define a component using the @component directive and render it using the @slot directive:

@component('alert')
    @slot('type', 'success')
    @slot('message')
        This is a success message.
    @endslot
@endcomponent

Using Blade Components:

Use Blade components in your views:

@extends('layouts.app')

@section('content')
    <x-alert type="success">
        This is a success message.
    </x-alert>
@endsection

Processing Dynamic Data in Blade Components:

Pass dynamic data to Blade components using the @props directive:

// create a component with dynamic props pre-defined like title, desc
@props(['title','desc'])
<div class="card">
    <div class="card-header">
        {{ $title }}
        {{ $desc}}
    </div>
    <div class="card-body">
        {{ $slot }}
    </div>
</div>

// pass the dynamic data using props in you blade file while using the component.
<x-card :title="your-title-here" :desc="your-description-here"/>

Rendering JSON Data:

You can render JSON data directly in Blade templates using the json_encode function:

@php
    $data = ['John', 'Doe', 'Jane', 'Smith'];
@endphp

<script>
    var jsonData = {!! json_encode($data) !!};
    console.log(jsonData);
</script>

You can get more details about this topic from here.

To get to know more about Laravel, you can check these articles too.

Conclusion:

Mastering Laravel Blade templates opens up a world of possibilities for creating dynamic and interactive views in your Laravel applications. By understanding the basics and exploring advanced techniques, you can streamline your development process and build elegant user interfaces with ease.

Please follow and like us:

Related Posts

Leave a Reply

Share