6
votes

I have searched SO, and dug in the Laravel documentation but I am not sure I quite understand if what I would like to do can be done.

I am using Laravel 4. I want to know how I can nest views in other views.

For example, I have a base layout.. lets call it layout.blade.php

<html>
    <head>
      <title>{{ $title }}</title>
    </head>
    <body>
        @yield('nav')  
        @yield('content')
    </body>
</html>

Next I have a blade for a page called home:

@extends('layout')

@section('nav')
<p>NAVIGATION</P>
@end

@section('content')
<p>HELLO WORLD!</P>
@end

I have a couple different navigation layouts, one for admins, another for super users, and another for regular users.

Is there a way to add another blade view inside the section('nav')?

@section('nav')
// do something magical here?
@end

It doesn't make sense that for every blade layout I need to repeat the navigation code when several snippets can be reused.

3

3 Answers

13
votes

You can do this

@section('nav')
  @include('another')
  @include('magical')
  @include('snippet')
@end
8
votes

Another solution, in case you were wishing to dynamically load different subviews, you can nest using the View Class. E.g. you could have the following in a Route / Controller:

return View::make('home')->nest('subnav','home/nav', array('some' => 'data'); 

and then in your home.blade.php, you could do this:

@extends('layout')

@section('nav')
<p>NAVIGATION</p>
{{ $subnav }}
@end

@section('content')
<p>HELLO WORLD!</p>
@end

This can be done with an include and a variable as well (@include($viewname, array('some' => 'data')) however I'd say its cleaner as it removes the logic from the view, particularly if your nested views aren't always the same blade file.

0
votes

Even though this is late you can also do this:

eg. in an admin.php you can have this:

@extends('home')

@section('nav')
  // navigation
@endsection

@section('content')
  // admin page content
@endsection

Not saying this is better or not i'm just answering your question on nesting views with blade, this is how i nest my views.