1

When templating in Laravel I'm aware of how to have a master template and define the individual content areas in each view file, but if I have a snippet of template code I want to include at certain points in my view files how would I do this?

To clarify, say I have a few views with a form in them. One might look like this:

@section('content')
    <h3>Login</h3>
{{ Form::open(array('url' => 'login')) }}

        <!-- if there are login errors, show them here -->
      @if ( count($errors->all()) )
      <div class="alert alert-danger">
      <p>Some validation errors occurred:</p>
      <ul>
         @foreach($errors->all() as $error)
         <li>{{ $error }}</li>
         @endforeach
      </ul>
      </div>
      @endif

    <div class="form-group">
        {{ Form::label('email', 'Email Address') }}
    etc...

As you can see there is an if statement checking for validation errors. This block is something I might want on many forms around my site but it seems bad practice to copy-paste the whole block across template files. Is there a way to store it in, say, a views/components.blade.php file and yield that particular block when required?

harryg
  • 23,311
  • 45
  • 125
  • 198

1 Answers1

2

your question is somehow the answer

@if( blabla )
    @include('components')
@endif
UX Labs
  • 1,481
  • 14
  • 29
  • 1
    Ha, yeah come to think of it, it is a pretty obvious answer. For some reason I was trying to think of using `yield` or something. Thanks – harryg Mar 05 '14 at 17:16
  • also try https://stackoverflow.com/questions/21753954/how-to-include-a-sub-view-in-blade-templates/21755728#21755728 – GWed Nov 15 '17 at 16:08