The main advantage of using Blade Templating Engine is that it provides template inheritance. With this, we can create a master layout page, which will become the base for other pages. So, the master page becomes the parent for all the child pages that we extend it to. IN this article we will cover the below topics:
1. Check for View Existence: To check if the view exists, there is a method called ‘exists()’.
Syntax
View::exists('gfg');
In place of ‘gfg’, you can specify the name of the view that you want to see it exists or not. This will output ‘true’ if the view exists and ‘false’ otherwise
Example:
Create a view in ‘resources/views’ directory with the name ‘gfg.balde.php’. Write the below code in it.
<!DOCTYPE html> <html> <head> <title>GeeksforGeeks</title> </head> <body> <h3>This is my View</h3> </body> </html>
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\View;
class GfGController extends Controller
{
public function index() {
if (View::exists('gfg')) {
return view('gfg');
}
else {
return 'No such view exist.';
}
}
}
?>
Route::get('/', 'GfGController@index');
2. Create the First Available View If there are many views in a Laravel application then using the ‘first()’ method, we can create the view that is available first.
Syntax
view()->first(['main', 'gfg', 'article']);
View::first(['main', 'gfg', 'article']);
Note: For this syntax, you will need to specify the View facade use Illuminate\Support\Facades\View; in the controller file.
Create a view in resources/viewsgfg.balde.php. Write the below code in it.
View::exists('gfg');<!DOCTYPE html>
<html>
<head>
<title>GeeksforGeeks</title>
</head>
<body>
<h3>This is the First Available View</h3>
</body>
</html>
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\View;
class GfGController extends Controller
{
public function index() {
return View::first(['main', 'gfg', 'article']);
}
}
?>
Route::get('/', 'GfGController@index');
My name is {{ $name }} and my age is {{ $age }}
My name is {!! $name !!} and my age is {!! $age !!}
My name is {!! $name !!} and my age is {!! $age !!}
Route::get('/', function () {
return view('gfg')->with('name' => 'Aakash')->with('age' => '21');
}
<!DOCTYPE html>
<html>
<head>
<title>GeeksforGeeks</title>
</head>
<body>
<h3>My name is {{ $name }} and my age is {{ $age }}</h3>
</body>
</html>