1
votes

How can I insert a view blade inside another view in blade engine in laravel

I am working in a project that's contains : home view ......"home.blade.php"

I want to add another page "home2.blade.php"

So finally I got 1 page which is "home.blade.php" but it contains "home2.blade.php"

this is the home2.blade.php

@extends('layouts.app')

@section('content')

<!-- All Posts -->
@if(count($posts) > 0)

<div class="panel panel-default">
    <div class="panel-heading">
        All Posts
    </div>

    <div class="panel-body">
        <table class="table table-striped task-table">

            <!-- Table Headings -->
            <thead>
                <th>Title</th>
                <th>Content</th>
            </thead>

            <!-- Table Body -->
            <tbody>
            @foreach($posts as $post)
                <tr>
                    <td class="table-text">
                        <div>{{$post->title}}</div>
                    </td>
                    <td class="table-text">
                        <div>{{$post->content}}</div>
                    </td>
                </tr>
            @endforeach
            </tbody>
        </table>
    </div>
</div>

  @endif

  @endsection

and this is "home.blade.php"

<html lang="en">
<head>
<meta charset="utf-8">

</head>

 <body data-spy="scroll" data-offset="0" data-target="#navigation">


  <!-- Fixed navbar -->
  <div id="navigation" class="navbar navbar-default navbar-fixed-top">
  <div class="container">
    <div class="navbar-header">
        <button type="button" class="navbar-toggle" data-toggle="collapse" 
  data-target=".navbar-collapse">
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
        </button>

    </div>
3
Please share the code of 'layouts.app' view. - Inzamam Idrees
You can look up the @include - kellymandem

3 Answers

0
votes

use include() in home.blade.php

Ex: if your home2 is located in resources/views/

@include('home2')
0
votes

Ok, this is what I tried and I can confirm that this works, at least for Laravel 5+ (I have L5.2). This is how I suggest you to use your blade templates.

Lets start saying that to yield a section into another section you have to define your included section before container section definition. So, with that clear, I solved this situation like this:

I got a main blade (main.blade.php) template which has something like:

<section class="content">
  <!-- Your Page Content Here -->
  @yield('main-content')
</section><!-- /.content -->

I got a second blade (common.blade.php) template which has that common stuff you may want to show across many pages and where main-content section is defined. This one looks like:

@section('main-content')
   <div class="container">
      @yield('extra-content')
   </div>
@endsection

Finally I got a 3rd template (test.blade.php) which extend the main template and include the common stuff I want to show, but be careful because the order matters. This one looks like:

@extends('main')
@section('extra-content')
   <div>
      <span> This is a test! </span>
   </div>
@endsection
@include('common')

In your controller or your route (wherever you return your view), you should return the 3rd template.

Hope this will help you

0
votes

you can use the @include('filename') directive. laravel documentation

<html lang="en">

<head>
    <meta charset="utf-8">

</head>

<body data-spy="scroll" data-offset="0" data-target="#navigation">

<!-- Fixed navbar -->
<div id="navigation" class="navbar navbar-default navbar-fixed-top">
    <div class="container">
        <div class="navbar-header">
            <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
            </button>

        </div>

        @include('home2') <-- your home2.blade.php file will be added here