Laravel 4 Blade #include variable - php

I was trying to do include with Laravel blade, but the problem is it can't pass the variable. Here's my example code:
file_include.blade.php
<?php
$myvar = "some text";
main.blade.php
#include('file_include')
{{$myvar}}
When I run the file, it return the error "Undefined variable: myvar". So, how can I pass the variable from the include file to the main file?
Thank you.

Why would you pass it from the include to the calling template? If you need it in the calling template, create it there, then pass it into the included template like this:
#include('view.name', array('some'=>'data'))
Above code snippet from http://laravel.com/docs/templates

Unfortunately Laravel Blade engine doesn't support what you expected!.But a little traditional way you can achieve this!
SOLUTION 1 - without Laravel Blade Engine
Step a:
from
file_include.blade.php
to
file_include.php
Step b:
main.blade.php
<?php
include('app/views/file_include.php')
?>
{{$myvar}}
SOLUTION 2 with Laravel Blade Engine
routes.php
$data = array(
'data1' => "one",
'data2' => "two",
);
View::share('data', $data);
Access $data array from Any View like this
{{ $data['data1'] }}
Output
one

Blade is a Template Engine for Laravel. So try passing the value from the controller or you may define it in the routes.php for testing purposes.
#include is used to include sub-views.

I think you must understand the variable scope in Laravel Blade template. Including a template using #include will inherit all variables from its parent view(the view where it was defined). But I guess you can't use your defined variables in your child view at the parent scope. If you want your variable be available to the parent try use View::share($variableName, $variableValue) it will be available to all views as expected.

In this scenarion $myvar would be available only on the local scope of the include call.
Why don't you send the variable directly from the controller?

I suggest you do a classic PHP require if you really want to change your variable (then it's automatically by reference)

Related

Access php variable defined in master blade in other blades where master blade is extended . PHP Laravel

I am working on a laravel PHP project and I have a global blade where I have defined a variable
<?php
$a = "some value fetched dynamically";
?>
And I am extending this blade in over 100 blade files like this
#extends('global')
In all these blades how can I access this $a variable?
Versions - PHP 7.2.34, Laravel 5.3.31
Thanks in advance.
PS: if there is any other way where I can get the PHP variable/function defined in the global blade in other blades please feel free to suggest.
Choose blade or layout you are importing in every blade and add this code
// AppServiceProvider.php
public function register()
{
// the layout you want, You can add '*' instead of 'blade' for all views
\View::composer('blade', function ($view) {
$value = "Data"
$view->with('value', $value);
});
}
At the blade use it like a normal variable.
Also, you can do it in another way Laravel composer

Laravel - How to pass data to include

So essentially all of my views are using the header.blade.php since I am including it in my master layout. I need to pass data to the header on every single view. Is there a way to pass data just to the include rather than passing the data for the header in each view?
You don't need to do that, but you can:
All variables that are available to the parent view will be made
available to the included view. 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'])
One option if you're trying to send data only to the included view is to use a view composer. They will fire even in the case of trying to prepare a view for #include
view()->composer('header', function($view) {
$view->with('data', 'some data');
});
actually the very best and faster method of sharing data to all views could be just using the
AppServiceProvider
instead of Jeff's answer you can use the share method instead of the composer method and achieve your goal faster.
Just pass the data you want in the boot method of the AppServiceProvider like following
public function boot()
{
View::share('key', 'value');
}
for more check this

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 }}

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.

Partial view inside a View?

In Zend Framework, is it possible to include (call) a Partial view from within a View?
Cheers,
From the Docs for Partial View Helper:
The Partial view helper is used to render a specified template within its own variable scope. The primary use is for reusable template fragments with which you do not need to worry about variable name clashes. Additionally, they allow you to specify partial view scripts from specific modules.
You would then call it from your view script using the following:
<?php echo $this->partial('partial.phtml', array(
'from' => 'Team Framework',
'subject' => 'view partials')); ?>
If you dont need the separate variable scope, you can simply call render('script.tpl') or include.
you can do that with the Action View Helper
<?php echo $this->action('list','comment'); ?>
Where list is the action and comment is the controller
More details http://framework.zend.com/manual/en/zend.view.helpers.html

Categories