Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nesting views with laravel Blade

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.

like image 720
Michael Avatar asked Jun 05 '13 22:06

Michael


People also ask

Is Laravel blade fast?

All views in Laravel are usually built in the blade template. Blade engine is fast in rendering views because it caches the view until they are modified.

What is the advantage of Laravel blade template?

The blade templates are stored in the /resources/view directory. The main advantage of using the blade template is that we can create the master template, which can be extended by other files.

What is @yield used for in Laravel?

In Laravel, @yield is principally used to define a section in a layout and is constantly used to get content from a child page unto a master page.

What are the two primary benefits of Laravel blade?

Two of the primary benefits of using Blade are template inheritance and sections. We can define a blade page as a combination of layout and sections. Since most of the general web applications will have the same layout across the web pages.


3 Answers

You can do this

@section('nav')
  @include('another')
  @include('magical')
  @include('snippet')
@end
like image 61
Antonio Carlos Ribeiro Avatar answered Oct 22 '22 09:10

Antonio Carlos Ribeiro


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.

like image 38
Bede Avatar answered Oct 22 '22 08:10

Bede


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.

like image 1
Blank EDjok Avatar answered Oct 22 '22 09:10

Blank EDjok