Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

laravel: how do I include partials from a blade layout?

I'd like to split my layouts into partials.

I'm creating a default blade layout.

The file is called default.blade.php, and lives in the layout folder.

Also under the layout folder is include. In this dir, I have head.blade.php

I would like to call 'head' from 'default'.

Every one of these fails and returns an error when debug is turned on:

@include('include/head'),  @include('include/head.blade.php'), @include('include/head.blade.php')

I have copied head.blade.php into the same folder as default.blade.php, and still get errors.

like image 246
dave dave Avatar asked May 11 '14 08:05

dave dave


People also ask

What is partials in Laravel?

Laravel Blade Partials A blade partial is similar to an include or require in PHP. It's an easy way to include contents of another file inside of a template. A PHP include would look something like 'include file. php' whereas a blade @include looks like @include('partials. file.

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 @stack in Laravel blade?

One of blade directive @stack('scripts') is very helpful when you have javascript need to execute in the child page. I create a fresh Laravel installation to demo an example. I have to make auth scaffolding with laravel/ui package because I'm using Laravel 6.

How do you add a line break in Laravel blade?

nl2br — Inserts HTML line breaks before all newlines in a string. Laravel helper provide many functionalities and you can use e Laravel helper function to purify your html before showing line breaks. You need to do the escaping first using e() and then after apply nl2br() function.


2 Answers

You need to give the full path relative to your view folder. Try doing this :

@include('layout.include.head')
like image 185
David Barker Avatar answered Sep 22 '22 13:09

David Barker


Keep in mind that Laravel uses dot notation to organize the templates and their parts.

So, the main.blade.php template in your main /views folder should be included directly:

@include ('main')

A main.blade.php template in a, e.g. /views/layouts folder should be called as:

@include ('layouts.main')

and so on.

like image 29
Cranio Avatar answered Sep 24 '22 13:09

Cranio