How to include a sub-view in Blade templates?
Asked Answered
Y

5

97

I am trying to set up a site using laravel, but I'm really having trouble with basic things that the documentation just doesn't cover.

In this case, I see that it says I can include one view inside another by using @include('view.name'). What is view.name? Where is it saved? I tried creating a file app/views/view.name.blade.php, but it wasn't read. How does the file name map to the blade name?

Yorgo answered 13/2, 2014 at 12:13 Comment(0)
M
37

EDIT: Below was the preferred solution in 2014. Nowadays you should use @include, as mentioned in the other answer.


In Laravel views the dot is used as folder separator. So for example I have this code

return View::make('auth.details', array('id' => $id));

which points to app/views/auth/details.blade.php

And to include a view inside a view you do like this:

file: layout.blade.php

<html>
  <html stuff>
  @yield('content')
</html>

file: hello.blade.php

@extends('layout')

@section('content')
  <html stuff>
@stop
Massotherapy answered 13/2, 2014 at 12:21 Comment(0)
S
292

You can use the blade template engine:

@include('view.name') 

'view.name' would live in your main views folder:

// for laravel 4.X
app/views/view/name.blade.php  

// for laravel 5.X
resources/views/view/name.blade.php

Another example

@include('hello.world');

would display the following view

// for laravel 4.X
app/views/hello/world.blade.php

// for laravel 5.X
resources/views/hello/world.blade.php

Another example

@include('some.directory.structure.foo');

would display the following view

// for Laravel 4.X
app/views/some/directory/structure/foo.blade.php

// for Laravel 5.X
resources/views/some/directory/structure/foo.blade.php

So basically the dot notation defines the directory hierarchy that your view is in, followed by the view name, relative to app/views folder for laravel 4.x or your resources/views folder in laravel 5.x

ADDITIONAL

If you want to pass parameters: @include('view.name', array('paramName' => 'value'))

You can then use the value in your views like so <p>{{$paramName}}</p>

Stays answered 13/2, 2014 at 13:31 Comment(6)
and to pass parameters: @include('partial', array("variable" => "value"))Tear
How can I include from any other folder than views?Hammer
You'd probably have to override one of the service providers in laravel with your own implementation. Unless you have a really good reason to change the view folder, I wouldn't bother: too much hassle for not much gain.Stays
@Sam This does not work, variable appears undefined in Laravel 5.3Biped
@Hammer you can set different folders, check this answerMckenna
upvoted. But one more thing . if you pass value from within a loop it looks like @include('layouts.polygon',[ 'field'=> $value]) and in new created blade for a template view to be used I removed <?php and then it worked.Caphaitien
M
37

EDIT: Below was the preferred solution in 2014. Nowadays you should use @include, as mentioned in the other answer.


In Laravel views the dot is used as folder separator. So for example I have this code

return View::make('auth.details', array('id' => $id));

which points to app/views/auth/details.blade.php

And to include a view inside a view you do like this:

file: layout.blade.php

<html>
  <html stuff>
  @yield('content')
</html>

file: hello.blade.php

@extends('layout')

@section('content')
  <html stuff>
@stop
Massotherapy answered 13/2, 2014 at 12:21 Comment(0)
F
15

As of Laravel 5.6, if you have this kind of structure and you want to include another blade file inside a subfolder,

|--- views

|------- parentFolder (Folder)

|---------- name.blade.php (Blade File)

|---------- childFolder (Folder)

|-------------- mypage.blade.php (Blade File)

name.blade.php

  <html>
      @include('parentFolder.childFolder.mypage')
  </html>
Foretop answered 23/4, 2018 at 2:23 Comment(0)
A
3

if file is not in sub folders

@include('view_name')

if file is in sub folders

@include('folder1.folder2.view_name')

Adoptive answered 11/5, 2023 at 9:14 Comment(0)
S
1

One more interesting situation with directory structure in laravel, following up on @gwed great answer:

@include('hello.world')

would display the following view:

// for laravel 5.X
resources/views/hello/world/index.blade.php
Silica answered 11/10, 2022 at 18:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.