View Partials in Laravel 5.1

Access all tutorials in sprocket icon.

June 17, 2015 from author Bill Keck.

How to make a view partial in Laravel 5.1.

Almost everything associated with using Laravel’s blade templating engine is incredibly easy. And this is true for creating view partials.

For those who are unfamiliar, a view partial is view that is meant to be called into another view. Common uses for this are form partials, where you have code that you don’t want to duplicated over and over. Also, view partials are handy for things like errors and alerts.

Here’a form view from my previous tutorial, Setting Up a Forgot Password Form and Controller in Laravel 5.1:



@extends('layouts.master')

@section('content')

<div class="container-fluid">
<div class="row">
<div class="col-md-8 col-md-offset-2">
<div class="panel panel-default">
<div class="panel-heading">Reset Password</div>
<div class="panel-body">
    @if (session('status'))
      <div class="alert alert-success">
             {{ session('status') }}
      </div>
    @endif

    @if (count($errors) > 0)
       <div class="alert alert-danger">
       <strong>Whoops!</strong> There were some problems with your input.<br><br>
       <ul>
           @foreach ($errors->all() as $error)
                    <li>{{ $error }}</li>
           @endforeach
        </ul>
        </div>
      @endif

<form class="form-horizontal" role="form" method="POST" action="/password/email">
<input type="hidden" name="_token" value="{{ csrf_token() }}">

<div class="form-group">
<label class="col-md-4 control-label">E-Mail Address</label>
<div class="col-md-6">
<input type="email" class="form-control" name="email" value="{{ old('email') }}">
</div>
</div>

<div class="form-group">
<div class="col-md-6 col-md-offset-4">
<button type="submit" class="btn btn-primary">
         Send Password Reset Link
</button>
</div>
</div>
</form>

</div>
</div>
</div>
</div>
</div>

@endsection


This is a form to request a reset link for your password. Notice the if statement:



   @if (count($errors) > 0)
       <div class="alert alert-danger">
       <strong>Whoops!</strong> There were some problems with your input.<br><br>
       <ul>
           @foreach ($errors->all() as $error)
                    <li>{{ $error }}</li>
           @endforeach
        </ul>
        </div>
      @endif

As you may already know, the $errors variable is always available to the view. So here we are iterating through them and displaying them if there are errors returned from the form input.

Now if you look at the other views associated with the forgot password controller, you can see that they all use the same exact code, which is code duplication and violates the DRY principle.

So one way to approach this is to make a partial view that just holds this code. If you have a new install of Larvel 5.1, it comes with an errors folder in the view folder, and this is the perfect place to put it.

So let’s create errors.blade.php in the errors folder and place the if statement in it as the only contents of that file.

When you are done with that, you can chop out the if statement from the other view, and in its place put:



@include('errors.errors')

You can see just how intuitive this syntax is. The file we need is in the errors folder and is named errors. Piece of cake!

Now, whenever you want to return the form errors, it’s one line of code.

From an organizational standpoint, you could create a partials folder inside of the view folder and place it there. There are lots of possibilities for this, so just do what is intuitive for you to work with.

Some people like to name their partials with an underscore in front, _errors.blade.php for example, but I don’t follow that practice.

Ok, so this was just a short tutorial on how to make a view partial in Laravel 5.1. I hope you enjoyed it. Click on the sprocket icon at the top of the page to see all tutorials. Please comment, share, and like if you can, thanks.

I don’t have a donate button, but If you would like to support my work, you can do so by buying one of my 99¢ books, I really appreciate it.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s