Laravel's Blade templating engine is inclue a powerful tool for building views in Laravel applications.
Blade templates are typically used for rendering HTML and other responses sent to the client.
1.Variable Echoing:
Blade uses the {{ }} syntax to echo variables in views. For example:
<p>Hello, {{ $name }}</p>
The value of the $name variable will be automatically escaped to prevent XSS attacks.
2.Raw Output:
If you need to output raw, unescaped HTML, you can use the {!! !!} syntax:
<p>{!! $html !!}</p>
3.Control Structures:
Blade supports common control structures like @if, @else, @elseif, @foreach, @for, and @while. Example:
@if($condition)
<p>This is true</p>
@else
<p>This is false</p>
@endif
4.Comments:
Blade allows you to add comments using the {{-- --}} syntax:
{{-- This is a Blade comment --}}
5.Including Sub-Views:
Blade provides the @include directive to include other views within a view:
@include('partials.header')
6.Extending Layouts:
Blade supports template inheritance with the @extends and @section directives.
Example:
Master layout (layouts/app.blade.php):
<html>
<head>
<title>@yield('title')</title>
</head>
<body>
@yield('content')
</body>
</html>
Child view:
@extends('layouts.app')
@section('title', 'Welcome')
@section('content')
<p>Hello, {{ $name }}</p>
@endsection
7.Stacks and Push:
Blade provides the @push and @stack directives for managing content stacks. Useful for scripts and styles in layouts:
@push('scripts')
<script src="your-script.js"></script>
@endpush
In the layout:
@stack('scripts')