SA__
SA__

Reputation: 1862

Declare a variable in the extended blade and get the value in another blade

How can I get the value that is declared inside the layout.blade.php file to another blade.php.

Here is my Code which is simplified to make you understand :

client.blade.php

<?php
$a = '33';
?>
@extends('layouts.layout')
@section('body')
<?php
echo $b;
?>

In the layout.blade.php :

<?php
echo $a;
$b = "hi";
?>

What happens is, I am able to print the variable $a in the layout.php file, but the variable $b that is declared inside the layout.blade.php is not printing in the client.blade.php.

How can I make available of it ?

Upvotes: 0

Views: 221

Answers (2)

Christian Giupponi
Christian Giupponi

Reputation: 7618

You can't do that.
You should use View::composer as suggested or use the View::share method that allow you to create a sort of global variable avaiable in all the views you need.

For example you can add a View::share('user', Auth::user()); in your BaseController and use $user in all the views rendered by controllers that extends the BaseController

Doc: http://laravel.com/docs/4.2/responses#views

Upvotes: 1

Laurence
Laurence

Reputation: 60048

I'm pretty sure you cant do that. The way the template works, you cant send data "back" the other way. You should declare the variable in your controller, and pass it to both views.

Or you should use a view composer, and have the variable automatically assigned when the view is created.

Upvotes: 1

Related Questions