Laravel Views - Laravel

What is the use of Laravel Views?

Understanding Views

Laravel Views separate the application logic and the presentation logic. Views are stored in resources/views directory. Views contain the HTML which will be served by the application.

Example

Step 1 − Copy the following code and save it at resources/views/test.php

Step 2 − To set the route for the above view, add the following line in app/Http/routes.php file.

app/Http/routes.php

Step 3 − Visit the following URL to see the output of the view.

http://localhost:8000/test

Step 4 − Output will appear as displayed below.

View output

Passing Data to Views

To pass data to the views is required when developing an application. Pass an array to view helper function. Use the key to get the value of that key after passing an array in the HTML file.

Example

Step 1 − Copy the following code and save in resources/views/test.php

Step 2 − Add the following line in app/Http/routes.php file to set the route for the above view.

app/Http/routes.php

Step 3 − Value of the key name will be passed to test.php file and $name will be replaced by that value.

Step 4 − Visit the following URL to see the output of the view.

http://localhost:8000/test

Step 5 − Output will appear as displayed below.

virat_gandhi

Sharing Data with all Views

Laravel “share()” is used to pass data to all the views. Laravel makes easier to pass the data to all the views by using this method. The share() method will take two arguments, key and value. This method can be called from boot method of any service provider. User can use any service provider like AppServiceProvider or own service provider

Example

Step 1 − Add the following line in app/Http/routes.php file.

app/Http/routes.php

Step 2 − Create two view files -test.php and test2.php with the same code. These files will be used for sharing data.

Copy the following code in both the files. resources/views/test.php & resources/views/test2.php

Step 3 − Change the code of boot method in the file app/Providers/AppServiceProvider.php as shown below. (Here, we have used share method and the data that we have passed will be shared with all the views.) app/Providers/AppServiceProvider.php

Step 4 − Visit the following URLs.

http://localhost:8000/test

http://localhost:8000/test2

Step 5 − Output will appear as displayed below.

virat_gandhi (1)

Blade Templates

Blade is a simple and great templating engine provided with Laravel. Blade is Laravel's lightweight template language and its syntax is very easy to learn. A blade template contains extension — blade.php and is stored at resources/views.

Blade supports all PHP's major constructs to create loops and conditions — @for, @foreach, @while, @if, and @elseif, allowing to avoid opening and closing the <?php tags everywhere in templates. The advantage of using Blade templates is that user can set up the master template, which master template can be extended by other individual pages.

Example

Step 1 − Create a master template and save it in resources/views/layouts/master.blade.php.

Step 2 − Here, in the master template,

  • @yield('title') is used to display the value of the title
  • @section('sidebar') is used to define a section named sidebar
  • @show is used to display the contents of a section
  • @yield('content') is used to display the contents of content

Step 3 – Let us now create another page and extend the master template and save it in resources/views/page.blade.php

Step 4 − Here is the description of each element.

@extends('layouts.master') is used to extend the master layout.“layouts.master” — Layouts is the name of the directory, where we stored the master template and “.master” of the master template“master.blade.php” refers to its name, here only name is used without extension blade.php

  • @section('title', 'Page Title') − sets the value of the title section.
  • @section('sidebar') − defines a sidebar section in the child page of master layout.
  • @parent − displays the content of the sidebar section, defined in the master layout.
  • <p> − This is appended to the master sidebar.</p> adds paragraph content to the sidebar section
  • @endsection − ends the sidebar section.
  • @section('content') − defines the content section.
  • @section('content') − adds paragraph content to the content section.
  • @endsection − ends the content section.

Step 5 – Let us set up the route to view this template by adding the following line at app/Http/routes.php

Step 6 − Visit the following URL to view the blade template example as below.

http://localhost:8000/blade

blade_template

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

Laravel Topics