[laravel] How to include a sub-view in Blade templates?

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?

This question is related to laravel blade templating

The answer is


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>

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>


When you use laravel modules, you may add the name's module:

@include('cimple::shared.posts_list')

Examples related to laravel

Parameter binding on left joins with array in Laravel Query Builder Laravel 4 with Sentry 2 add user to a group on Registration Target class controller does not exist - Laravel 8 Visual Studio Code PHP Intelephense Keep Showing Not Necessary Error The POST method is not supported for this route. Supported methods: GET, HEAD. Laravel How to fix 'Unchecked runtime.lastError: The message port closed before a response was received' chrome issue? Post request in Laravel - Error - 419 Sorry, your session/ 419 your page has expired Expected response code 250 but got code "530", with message "530 5.7.1 Authentication required How can I run specific migration in laravel Laravel 5 show ErrorException file_put_contents failed to open stream: No such file or directory

Examples related to blade

How to access URL segment(s) in blade in Laravel 5? Lumen: get URL parameter in a Blade view Laravel: How do I parse this json data in view blade? Switch in Laravel 5 - Blade Laravel-5 how to populate select box from database with id value and name value Laravel 5: Display HTML with Blade Define the selected option with the old input in Laravel / Blade Laravel 5 call a model function in a blade view Displaying the Error Messages in Laravel after being Redirected from controller How to include a sub-view in Blade templates?

Examples related to templating

How to include a sub-view in Blade templates? Passing variables through handlebars partial How to concatenate strings in twig Bash Templating: How to build configuration files from templates with Bash? Is there any way to return HTML in a PHP function? (without building the return value as a string) How to replace ${} placeholders in a text file?