92

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?

GWed
  • 15,167
  • 5
  • 62
  • 99
Benubird
  • 18,551
  • 27
  • 90
  • 141

5 Answers5

286

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>

GWed
  • 15,167
  • 5
  • 62
  • 99
  • 4
    and to pass parameters: @include('partial', array("variable" => "value")) – S.. Nov 09 '14 at 17:02
  • 2
    How can I include from any other folder than `views`? – itsazzad Feb 09 '15 at 03:46
  • 2
    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. – GWed Feb 09 '15 at 15:43
  • @Sam This does not work, `variable` appears undefined in Laravel 5.3 – Volatil3 Oct 02 '16 at 06:20
  • @itsazzad you can set different folders, check this [answer](http://stackoverflow.com/a/28848517/1536172) – Scofield Dec 12 '16 at 12:12
  • 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 ` – CodeToLife Jan 05 '21 at 20:01
36

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
GWed
  • 15,167
  • 5
  • 62
  • 99
winkbrace
  • 2,682
  • 26
  • 19
14

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>
JonathanDavidArndt
  • 2,518
  • 13
  • 37
  • 49
Kent
  • 437
  • 1
  • 6
  • 10
1

if file is not in sub folders

@include('view_name')

if file is in sub folders

@include('folder1.folder2.view_name')

PQ RS
  • 41
  • 3
0

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
Kamlesh
  • 5,233
  • 39
  • 50
Jorge Mauricio
  • 411
  • 6
  • 18