How to include a view.blade.php file in Laravel? - php

I have this file sidebar.blade.php inside views/content, I want to include this (sidebar.blade.php) file to my views home.blade.php.

Did you look at the docs?
Blade's #include directive, allows you to easily include a Blade view
from within an existing view. All variables that are available to the
parent view will be made available to the included view:
#include('shared.errors')
Even though the included view will inherit all data available in the
parent view, you may also pass an array of extra data to the included
view:
#include('view.name', ['some' => 'data'])

Related

Not having to extend and section in every file Laravel/Blade

Is it possible to make every view extend my main layout without having to put #extends('layout') and #section('content') in every file at the top? For example by using a provider? I'm new to Laravel and Blade.
Actually its better to use extend but in case you want to get rid of #extends or #section, you can always return your main layout from controller and pass views as parameters to the main layout.
mainlayout.blade.php:
//replace yield directive with content variable
// #yield('content')
{!!$content!!}
in controller:
//return view('test');
$content=view('test')->render();
return view('mainlayout',['content'=>$content]);
Now in test view you don't need #extends and #section.

Addd partial blade view template to all views?

I have a partial view with a script I want to include in all my views, I dont't wanna #include this partial in all my layouts and blade views but instead add it to all rendered views, my partial looks like this:
<script type="text/javascript">window.$app = {!! json_encode(app(App\Helpers\Javascript::class)->app) !!};</script>
How can I do this?
You could:
include it in your main(s) layout template (the one(s) handling <html> and <body> tags)
you could use a Laravel View Composer to add the JSON data to the required views. Those views would so include the JSONed data, that you'd echo in the right place to be handled by your JS.
you could check the Php-Vars-To-Js package that allows you to put some data from PHP in a custom JS namespace (eg window.yourNameSpace). And then use it from a View Composer or the specific controller's methods concerned by your views.

Laravel Blade - pass variable via an #include or #yield

I need to pass a variable to an included Blade file. I have attempted this two-ways; however, neither have been successful.
Pass a variable, title, to the included file:
#section('left')
#include('modal', ['title' => 'Hello'])
#stop
Use #yield and set the section:
#section('left')
#include('modal')
#section('title')
Hello
#stop
#stop
I am using Laravel 4.2. I am unaware if what I am trying to do is possible, but I imagine it is.
According to the documentation, the include-way should be the way to do it:
Including Sub-Views
#include('view.name')
You may also pass an array of data to the included view:
#include('view.name', array('some'=>'data'))
My hunch is that $title is conflicting with another variable in your nested templates. Just for troubleshooting, try temporarily calling it something else.
pass an array of data to the included view
#include('view.name', array('some'=>'data'))
then use this on view/name folder
{{ $some }}

Blade template yield with default include

Is it possible to yield to another view but then default with an include statement?
Example:
#yield('header', #include('partials.header'))
The intended action is that if the subview does not include the header, then the current template will include the partials.header view.
It is possible. But you can't use blade tags inside blade tags. What you can do however is use View::make() instead of #include. #include actually compiles to a make call on the view factory.
#yield('header', View::make('partials.header'))

How does Blade #include work with variables?

I just started working with Laravel / Blade a few weeks ago and was wondering how Blade #include works.
I have a top level index view that then includes some other views. These other views require specific variables, which I know you can pass in through #include.
I also have a controller which creates the top level view. The controller is where I pass in the variables. And it seems that once they've been passed in to the top view, I don't need to pass them in to the sub views.
For a visual
controller
View::make('index', array('abc' => $abc))
index.blade.php
#include('sub.piece') - Do I still need to pass in the array with abc here?
sub/piece.blade.php
{{ abc }}
Does #include work like other includes where it's essentially a copy paste?
You do not have to pass in data that is already available to the parent view.
Passing data in #include is useful for when the variable name differs for the two views.

Categories