Php

champak
₹2,000.00 ₹1,500.00
  • 15 students
  • 11 lessons
  • 0 quizzes
  • 10 week duration

Php

Templating in Laravel —1

Templating starts with developing a html page in the resources/views directory and returning it through the routes.


<html>
<head>
<title>Champak's World - Client Page</title>
</head>
<body>

<div class="container">
Client Content
</div>
<div class="container">
Footer
</div>
</body>
</html>


Route::get('/client', function () {
return view('client');
});

 

Passing data from route to view. Just put the data in an array and pass it.


Route::get('/client', function () {
return view('client' ,['name' => 'Champak']);
});


<html>
<head>
<title>Champak's World - Client Page</title>
</head>
<body>

<div class="container">
Client Content<br>
The Name is {{$name}}
</div>
<div class="container">
Footer
</div>
</body>
</html>

&nbsp;

By default the blade templating engine will escape html tags. Thus the following route will not display the name in h1 mode.


Route::get('/client', function () {
return view('client' ,['name' => '<h1>Champak</h1>']);
});
?>

Output

This will display it as html


<html>
<head>
<title>Champak's World - Client Page</title>
</head>
<body>

<div class="container">
Client Content<br>
The Name is {!!$name!!}
</div>
<div class="container">
Footer
</div>
</body>
</html>

Sending an associative array to the template.


Route::get('/client', function () {
$names=array("Sachin"=>"150", "Dhoni"=>"200", "Rahul"=>"300");
return View::make('client')->with('names', $names);
//return view('client')->with('names',);
});


<html>
<head>
<title>Champak's World - Client Page</title>
</head>
<body>

<div class="container">
Client Content<br>
<table border="1">
@foreach($names as $key => $value)
<tr>
<td>{{ $key }}</td>
<td>{{ $value }}</td>
</tr>

@endforeach
</table>
</div>
<div class="container">
Footer
</div>
</body>
</html>

&nbsp;

end

Leave a Reply

Your email address will not be published. Required fields are marked *