Skip to main content

View

  • Blade Template
  • Blade Engine
  • Variable
  • Yield Directive
  • Extends Directive
  • Passing object parameter

Blade Template

  • stored in the resources/views directory
  • use the .blade.php file extension
  • variables are wrapped in curly braces

Blade Engine

  • a simple but powerful templating engine
  • compiles blade templates into plain PHP code and cached until they are modified route-laravel-bladeengine

yield directive

  • in the views folder, create a layout.blade.php file
  • yield is a placeholder where we could later replace with actual data using @section
<html>
<title>@yield('title')</title>
<body><h1>@yield('content')</h1>
</body>
</html>

@extends directive

  • in the views folder, create a index.blade.php file
@extends('layout') //include the layout.blade.php file
<@section('title','homepage') //replace yield with section display text only
<@section('content') //replace yield with section display html
<h1>Hello World!</h1>
<@endsection

Passing object parameter

  • Assuming we have an object passed from the routes/web.php
Route::get('/posts/{id}',function($id){ //passing an object
$posts = [1=>['title'=>'HTML','content'=>'intro to HTML'],2=>['title'=>'PHP','content'=>'intro to PHP']];
return view('posts',[ 'post'=> $posts[$id] ]);
});
in the views folder, create a posts.blade.php file
@extends('layout')
@section('title',$post['title'])
@section('content')
<h1>{{post['title']}}</h1>
<p>{{post['content']}}</p>
@endsection