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.
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.
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.
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.
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.
You can do this
@section('nav')
@include('another')
@include('magical')
@include('snippet')
@end
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With