X
    Categories: Learn Laravel Tutorials, Tips And Guides
Reading Time: 9 minutes

Creating the layout is an important part of any project. Realizing this, Laravel comes with Blade templating engine which generates HTML based sleek designs and templates. All Laravel views built using Blade are located in resources/views.

Bootstrap is well known in the development circles for impressive design options. Laravel makes it incredibly easy to use Bootstrap templates in project’s views. In this tutorial, I will demonstrate how Bootstrap templates could be used within Laravel Blade engine.

Integrating Bootstrap template with Laravel is a simple process. All you need to do is cut your HTML Bootstrap into tiny Blade template contents, and then use, extend and/or include the templates in the main Blade file. To demonstrate the process, I will use  Album Example For Bootstrap

A simple prerequisite is a fresh installation of Laravel 5.5.

Create Layout File

Before creating the layout file, you need to understand the Laravel file structure. For this, I recommend reading my article on creating Laravel layouts using Blade templating engine.

Now let’s take a look at the code of the template page and identify the different parts of the page. Keep these parts in separate files for easy management.

Now as you can see the code of your template. It consists of different parts and I will create a layout in which I’ll keep all the partial parts of the layout separate.

Go to resources/view folder and create a new folder with the name, layouts. This folder will contain the main layout (and other files that will be included in the layout).

Now, create another folder and name it partials. This folder will contain partial files such as header and footer that will be used by the layout file.

Go to the layout folder and create a file named mainlayout.blade.php. Add the following code to it:

<!DOCTYPE html>

<html lang="en">

 <head>

   @include('layout.partials.head')

 </head>



 <body>



@include('layout.partials.nav')



       @include('layout.partials.header')



@yield('content')



@include('layout.partials.footer')

@include('layout.partials.footer-scripts')




 </body>

</html>



<!DOCTYPE html>

<html lang="en">

 <head>

   @include('layout.partials.head')

 </head>

 <body>

@include('layout.partials.nav')

       @include('layout.partials.header')

@yield('content')

@include('layout.partials.footer')

@include('layout.partials.footer-scripts')

 </body>

</html>

This code creates a layout file that will include (@include) all the contents of the specified file at the desired location inside the HTML file. @yield will put the specified content of the file which is extending this layout.

Create Partial Files

Next, create partial files that are included in the main layout file. Go to the partials folder and create a file named head.blade.php. This file will contain the content that goes into the head section of the page.

<meta charset="utf-8">

<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

<meta name="description" content="">

<meta name="author" content="">



<title>Album example for Bootstrap</title>



<!-- Bootstrap core CSS -->

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/css/bootstrap.min.css" integrity="sha384-rwoIResjU2yc3z8GV/NPeZWAv56rSmLldC3R/AZzGRnGxQQKnKkoFVhFQhNUIyJ" crossorigin="anonymous">




<!-- Custom styles for this template -->

<link href="/css/album.css" rel="stylesheet">

<meta charset="utf-8">

<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

<meta name="description" content="">

<meta name="author" content="">

<title>Album example for Bootstrap</title>

<!-- Bootstrap core CSS -->

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/css/bootstrap.min.css" integrity="sha384-rwoIResjU2yc3z8GV/NPeZWAv56rSmLldC3R/AZzGRnGxQQKnKkoFVhFQhNUIyJ" crossorigin="anonymous">

<!-- Custom styles for this template -->

<link href="/css/album.css" rel="stylesheet">

Create a file nav.blade.php . This file will contain the code related to creating the navigation of the bootstrap page

 <div class="collapse bg-inverse" id="navbarHeader">

     <div class="container">

       <div class="row">

         <div class="col-sm-8 py-4">

           <h4 class="text-white">About</h4>

           <p class="text-muted">Add some information about the album below, the author, or any other background context. Make it a few sentences long so folks can pick up some informative tidbits. Then, link them off to some social networking sites or contact information.</p>

         </div>

         <div class="col-sm-4 py-4">

           <h4 class="text-white">Contact</h4>

           <ul class="list-unstyled">

             <li><a href="#" class="text-white">Follow on Twitter</a></li>

             <li><a href="#" class="text-white">Like on Facebook</a></li>

             <li><a href="#" class="text-white">Email me</a></li>

           </ul>

         </div>

       </div>

     </div>

   </div>

   <div class="navbar navbar-inverse bg-inverse">

     <div class="container d-flex justify-content-betIen">

       <a href="#" class="navbar-brand">Album</a>

       <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarHeader" aria-controls="navbarHeader" aria-expanded="false" aria-label="Toggle navigation">

         <span class="navbar-toggler-icon"></span>

       </button>

     </div>

   </div>



   <div class="collapse bg-inverse" id="navbarHeader">

     <div class="container">

       <div class="row">

         <div class="col-sm-8 py-4">

           <h4 class="text-white">About</h4>

           <p class="text-muted">Add some information about the album below, the author, or any other background context. Make it a few sentences long so folks can pick up some informative tidbits. Then, link them off to some social networking sites or contact information.</p>

         </div>

         <div class="col-sm-4 py-4">

           <h4 class="text-white">Contact</h4>

           <ul class="list-unstyled">

             <li><a href="#" class="text-white">Follow on Twitter</a></li>

             <li><a href="#" class="text-white">Like on Facebook</a></li>

             <li><a href="#" class="text-white">Email me</a></li>

           </ul>

         </div>

       </div>

     </div>

   </div>

   <div class="navbar navbar-inverse bg-inverse">

     <div class="container d-flex justify-content-betIen">

       <a href="#" class="navbar-brand">Album</a>

       <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarHeader" aria-controls="navbarHeader" aria-expanded="false" aria-label="Toggle navigation">

         <span class="navbar-toggler-icon"></span>

       </button>

     </div>

   </div>

Next, create the file header.blade.php. This file will contain the visible header of the Bootstrap page.

<section class="jumbotron text-center">

     <div class="container">

       <h1 class="jumbotron-heading">Album example</h1>

       <p class="lead text-muted">Something short and leading about the collection below—its contents, the creator, etc. Make it short and sIet, but not too short so folks don't simply skip over it entirely.</p>

       <p>

         <a href="#" class="btn btn-primary">Main call to action</a>

         <a href="#" class="btn btn-secondary">Secondary action</a>

       </p>

     </div>

</section>

<section class="jumbotron text-center">

     <div class="container">

       <h1 class="jumbotron-heading">Album example</h1>

       <p class="lead text-muted">Something short and leading about the collection below—its contents, the creator, etc. Make it short and sIet, but not too short so folks don't simply skip over it entirely.</p>

       <p>

         <a href="#" class="btn btn-primary">Main call to action</a>

         <a href="#" class="btn btn-secondary">Secondary action</a>

       </p>

     </div>

</section>

The next step is to create the file for a footer. For this, create a file named footer.blade.php.

<footer class="text-muted">

     <div class="container">

       <p class="float-right">

         <a href="#">Back to top</a>

       </p>

       <p>Album example is &copy; Bootstrap, but please download and customize it for yourself!</p>

       <p>New to Bootstrap? <a href="../../">Visit the homepage</a> or read our <a href="../../getting-started/">getting started guide</a>.</p>

     </div>

   </footer>

   <footer class="text-muted">

     <div class="container">

       <p class="float-right">

         <a href="#">Back to top</a>

       </p>

       <p>Album example is &copy; Bootstrap, but please download and customize it for yourself!</p>

       <p>New to Bootstrap? <a href="../../">Visit the homepage</a> or read our <a href="../../getting-started/">getting started guide</a>.</p>

     </div>

   </footer>



Now create a file named footer-scripts.blade.php that contains the JS files that should be included in the bottom of the page.

<!-- Bootstrap core JavaScript

================================================= -->

<!-- Placed at the end of the document so the pages load faster -->

<script src="https://code.jquery.com/jquery-3.1.1.slim.min.js" integrity="sha384-A7FZj7v+d/sdmMqp/nOQwliLvUsJfDHW+k9Omg/a/EheAdgtzNs3hpfag6Ed950n" crossorigin="anonymous"></script>

<script src="https://cdnjs.cloudflare.com/ajax/libs/tether/1.4.0/js/tether.min.js" integrity="sha384-DztdAPBWPRXSA/3eYEEUWrWCy7G5KFbe8fFjk5JAIxUYHKkDx6Qin1DkWx51bBrb" crossorigin="anonymous"></script>

<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/js/bootstrap.min.js" integrity="sha384-vBWWzlZJ8ea9aCX4pEW3rVHjgjt7zpkNpZk+02D9phzyeVkE+jo0ieGizqPLForn" crossorigin="anonymous"></script>



<!-- Bootstrap core JavaScript

================================================= -->

<!-- Placed at the end of the document so the pages load faster -->

<script src="https://code.jquery.com/jquery-3.1.1.slim.min.js" integrity="sha384-A7FZj7v+d/sdmMqp/nOQwliLvUsJfDHW+k9Omg/a/EheAdgtzNs3hpfag6Ed950n" crossorigin="anonymous"></script>

<script src="https://cdnjs.cloudflare.com/ajax/libs/tether/1.4.0/js/tether.min.js" integrity="sha384-DztdAPBWPRXSA/3eYEEUWrWCy7G5KFbe8fFjk5JAIxUYHKkDx6Qin1DkWx51bBrb" crossorigin="anonymous"></script>

<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/js/bootstrap.min.js" integrity="sha384-vBWWzlZJ8ea9aCX4pEW3rVHjgjt7zpkNpZk+02D9phzyeVkE+jo0ieGizqPLForn" crossorigin="anonymous"></script>


After creating all the views files, the views folder should look something like this:

That’s it, The sample Bootstrap template Album Bootstrap is now integrated with Laravel.

Testing the Integration

Let’s create a view file that extends this layout file, so that you can see the Bootstrap layout integration with Laravel Blade template in action.

Create a file demo.blade.php in the views folder and add the following content to the file:

@extends('layout.mainlayout')



@section('content')

&nbsp;&nbsp;&nbsp;<div class="album text-muted">

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<div class="container">



&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<div class="row">

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<h1>This is a demo text</h1>

&nbsp;<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed egestas dolor vulputate quam convallis consequat. Quisque eu lorem eget magna lacinia suscipit. Maecenas condimentum vehicula eros. Fusce massa lacus, blandit et leo sed, accumsan commodo sem. Sed eget pulvinar tellus. Praesent ex diam, sodales at consequat id, viverra ut dolor. In eget orci sit amet magna sagittis mattis sit amet sed augue. Vivamus facilisis libero ligula, vel sodales ipsum sollicitudin id. Duis vitae urna rutrum, dignissim arcu ac, elementum augue. Quisque id interdum ligula. Donec tincidunt feugiat massa sed aliquam. Duis eu vehicula turpis.</p>

</div>

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</div>

&nbsp;&nbsp;&nbsp;</div>

@endsection


@extends('layout.mainlayout')

@section('content')

   <div class="album text-muted">

     <div class="container">

       <div class="row">

         <h1>This is a demo text</h1>

  <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed egestas dolor vulputate quam convallis consequat. Quisque eu lorem eget magna lacinia suscipit. Maecenas condimentum vehicula eros. Fusce massa lacus, blandit et leo sed, accumsan commodo sem. Sed eget pulvinar tellus. Praesent ex diam, sodales at consequat id, viverra ut dolor. In eget orci sit amet magna sagittis mattis sit amet sed augue. Vivamus facilisis libero ligula, vel sodales ipsum sollicitudin id. Duis vitae urna rutrum, dignissim arcu ac, elementum augue. Quisque id interdum ligula. Donec tincidunt feugiat massa sed aliquam. Duis eu vehicula turpis.</p>

</div>

     </div>

   </div>

@endsection






 

Create the Routes

Now that the view is created, the next step is the creation of a route to access the view. For this, go to routes/web.php and paste the following routes.

Route::get('/demo', function () {

   return view('demo');

});

Route::get('/demo', function () {

   return view('demo');

});

Now run the application using the staging URL. You will see the Album Bootstrap template integrated with your application.

To Sum Up

In this tutorial, I have demonstrated an easy way of integrating any Bootstrap template in a Laravel application. If you have any questions kindly comment below. Also, follow me on Twitter and get connected.

Saquib Rizwan :Saquib is a PHP Community Expert at Cloudways - A Managed PHP Hosting Cloud Platform. He is well versed in PHP and regularly contributes to open source projects. For fun, he enjoys gaming, movies and hanging out with friends. You can email him at saquib.rizwan@cloudways.com