Blade templating
Blade is the default templating engine in Laravel, a popular PHP web application framework. It is a lightweight, fast, and easy-to-use engine that allows you to build dynamic views for your web application.
Here is a tutorial on how to use Blade in Laravel, including code examples:
- First, make sure that you have Laravel installed on your development machine. You can use the Composer dependency manager to install Laravel and create a new project.
- In your Laravel project, you will find a “resources” directory that contains all of your application’s views. By default, these views are stored as PHP files, but you can use Blade to create more dynamic views.
- To use Blade, you need to use the .blade.php file extension instead of .php. For example, you might create a view file called “home.blade.php”.
- Blade provides a number of directives that you can use in your views to add dynamic content and control the flow of your code. Some common directives include:
@if
and@elseif
for conditional statements@foreach
for looping through collections of data@include
for including other views or templates@extends
for creating a layout that can be extended by other views
Here are some code examples to illustrate how to use these directives:
Example 1: Conditional statements
@if ($age > 18)
You are an adult.
@else
Example 2: Looping through collections
@foreach ($users as $user)
<p>{{ $user->name }}</p>
@endforeach
Example 3: Including other views or templates
@include('partials.header')
Example 4: Creating a layout that can be extended by other views
<!-- layout.blade.php -->
<html>
<head>
<title>My Website</title>
</head>
<body>
@yield('content')
</body>
</html>
<!-- home.blade.php -->
@extends('layout')
@section('content')
<h1>Welcome to my website!</h1>
@endsection
- To pass data from your controller to your view, you can use the
return view('view_name', $data)
method in your controller, where$data
is an array of data that you want to pass to the view. You can then access this data in your view using the$
symbol, followed by the key of the data item you want to access. For example, if you pass an array with a key of “name”, you can access it in your view using$name
.
Here is an example of how to pass data from a controller to a view:
// In your controller
public function showProfile($id)
{
$user = User::find($id);
return view('profile', ['user' => $user]);
}
<!-- profile.blade.php -->
<h1>{{ $user->name }}</h1>
<p>{{ $user->email }}</p>
- You can also use PHP code in your Blade views, just like you would in a regular PHP file. However, you should avoid using PHP tags and instead use Blade directives to control the flow of your code.
For example, instead of using a PHP for
loop, you can use the @for
directive:
@for ($i = 0; $i < 10; $i++)
<p>{{ $i }}</p>
@endfor
- Once you have created your views using Blade, you can use the
route
andview
helpers in your controllers to render the views and pass data to them.
Here is an example of how to render a view and pass data to it in a controller:
public function showHomePage()
{
$products = Product::all();
return view('home', ['products' => $products]);
}
I hope this tutorial and the accompanying code examples have helped you get started with using Blade in Laravel.