Jeff P.
Jeff P.

Reputation: 3004

Laravel PHP: Having trouble using nest()

I'm trying to use 'nest()' in Laravel PHP by taking 2 views, 1) The layout (layouts\master.blade.php) and 2) The form (forms\new-video.php).

However when I use the 'nest()' function, all I am seeing is the output from 'master.blade.php' and not the form from 'forms.new-video.blade.php'.

Controller:

class VideosController extends BaseController {

/**
 * The video model
 *
 * @var \Models\Video
 **/
protected $video;


//  Use this variable for the repository

protected $videoRepo;

protected $layout = 'layouts.master';

  public function create()
  {
    $data = array('type' => 'video');
    $this->layout->content = \View::make('forms.new-video', $data)
        ->nest('form', 'forms.new-video');
  }
}

EDIT: Here is my 'master.blade.php' layout(template):

<!doctype html>
<html>
<head>
    <link href="//netdna.bootstrapcdn.com/bootstrap/3.1.0/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href=" <?php echo asset('css/style.css')?>" type="text/css">
    <style type="text/css">
        form ul {list-style: none}
        .navbar {border-radius: 0px;}
    </style>
</head>

<body>
    <nav class="navbar navbar-inverse" role="navigation">
        <div class="container-fluid">
            <!-- Brand and toggle get grouped for better mobile display -->
            <div class="navbar-header">
              <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
                <span class="sr-only">Toggle navigation</span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
              </button>
              <a class="navbar-brand" href="#">Pictures and Videos</a>
            </div>
            <div class="collapse navbar-collapse">
                <ul class="nav navbar-nav">
                    <li {{ (Request::is('pics_vids') ? 'class="active"' : '') }}>{{ link_to_route('overview', 'Home') }}</li>
                    <li {{ (Request::is('pics_vids') ? 'class="active"' : '') }}>{{ link_to_route('overview', 'Home') }}</li>
                    <li {{ (Request::is('pics_vids/new_video') ? 'class="active"' : '') }}>{{ link_to_route('new_video', 'New Video') }}</li>
                </ul>
            </div>
        </div>
    </nav>

/* Content is being called in my 'container' <div> */

    <div class="container">
        @if (Session::has('message'))
            <div class="flash alert">
                <p>{{ Session::get('message') }}</p>
            </div>
        @endif

/* Call up the content here  */

        @yield('content')
    </div>

    <script src="//code.jquery.com/jquery-1.11.0.min.js"></script>
    <script src="//netdna.bootstrapcdn.com/bootstrap/3.1.0/js/bootstrap.min.js"></script>
</body>

I know I'm not using the 'nest()' method properly since Laravel is still new to me. Any help is appreciated!

Upvotes: 0

Views: 228

Answers (1)

Bogdan
Bogdan

Reputation: 44536

You can use layouts in 2 main ways in Laravel:

1. Controller Templates

In your controller you have:

$this->layout->content = \View::make('forms.new-video', $data);

In your master.blade.php you have this where you want the content to be placed:

{{ $content }} // When using controller templates you don't use @yield

And in your new-video view you have whatever html you need.

2. Template Inheritance

You controller doesn't need to extend BaseController for this to work. And in your action you return the view not assign it, like so:

return \View::make('forms.new-video', $data);

In your master.blade.php you use @yield where you want to include your content:

@yield('content');

In your new-video view you need to specify that the view @extents your master layout and define a section named content that will be included in your layout:

@extends('layouts.master')

@section('content')
    // Your form html goes here
@stop

In your case you can just use $this->layout->content = \View::make('forms.new-video', $data); in your controller and {{ $content }} instead of @yield in your master layout.

Upvotes: 2

Related Questions