Can't see variable inside content view in Laravel 4 with Blade - laravel

I have a problem understanding how variables work inside the Laravel templating system, Blade.
I set the variables in the controller, I can see them in the first view I make, but not inside the next one.
I'm using a master.blade.php file that holds the header and footer of the page, yielding the content in the middle. I can use the variable inside the master.blade.php file, but not inside the content blade file (variable undefined).
For example, for the contact page:
CONTROLLER FUNCTION:
$this->data['page'] = "contact";
$this->layout->content = View::make('pages.contact');
$this->layout->with('data', $this->data);
MASTER.BLADE.PHP:
if ($data['page'] == 'contact')
{ //do something, it works }
#yield('content')
CONTACT.BLADE.PHP:
if ($data['page'] == 'contact')
{// do something. ErrorException Undefined variable: data}
Am I missing something or is it a known restriction?
Thanks!

The problem was that I was passing the variables only to the layout:
Instead of:
$this->layout->content = View::make('pages.contact');
$this->layout->with('data', $this->data);
I fixed it using:
$this->layout->content = View::make('pages.contact', array('data' => $this->data));
$this->layout->with('data', $this->data);
This way passing the variables to the layout, but also to that particular view.
Hope it helps someone.

Related

laravel 5.2 carousel only in index page doesn't wok

I use laravel 5.2 and tried to display a carousel only in index page but doesn't work.
I chose that the codes are not "spreaded" on the index page, they are stored in: public/carousel/carousel.php, too(not ...blade.php).
route.php:
Route::get('/', 'HomeController#index')
HomeController.php:
{
$cats = Category::all();
$carousel = public_path('carousel/carousel.php');
//$carousel = storage_path('public/carousel/carousel.php');
return view('layouts.app', compact('cats', 'carousel'));
}
layouts/app.blade.php:
{{-- #include('carousel/carousel');--}}
#if($carousel)
{{ $carousel }}
#endif
#yield('content')
Finally it displays only: C:\wamp\www\app_name\public\carousel/carousel.php.
Can you help me or point to another better way?
In your controller, you are passing to the view a variable called $carousel, which is the path to your file, as you defined here:
$carousel = public_path('carousel/carousel.php');
This is the reason why it only displays the string. You need to get the actual content of the file:
$carousel = file_get_content(public_path('carousel/carousel.php'));
A better and more laravel-ish way to do it would be to rename the file to carousel.blade.php, store it into the resources/views folder and simply include it from your main blade file (without the need of doing anything in the controller):
#include('carousel')
If you need to display the carousel on certain pages only, you can simply pass a variable $carousel = true on the pages that needs to display it:
$carousel = true;
return view('layouts.app',compact('carousel'));
And in your blade view, include the carousel file only when this variables is present and is true:
#includeWhen(isset($carousel) && $carousel, 'carousel')

Laravel 5.3 parse variable from controller to blade view not working

I cannot seed my blade with variables from my controller.
Look at my controller:
public function formAdd()
{
return view('restaurant.categoryAdd')->with(['myvar' => 'xxx']);
}
In my view categoryAdd:
{{$myvar}}
This way, I got a 500 error. But if I change my view to:
#if (isset($myvar))
<p>Success!</p>
#endif
There is not 500 error, the page is ok, but the 'Success!' is not returned. It means $myvar is undefined. I tested various codes, but I still cannot seed my view.
I tried:
return view('restaurant.categoryAdd')->with(['myvar' => 'xxx']);
return view('restaurant.categoryAdd')->with('myvar', 'xxx');
return view('restaurant.categoryAdd',['myvar' => 'xxx']);
obs: I'm running XAMPP, and It's the only issue I had at this time.
obs2: The problem is not the 'return view' method, It's just working. The problem is my view, It's not receiving the variable.
So try this:
In your controller:
public function formAdd() {
$variable = 'Hello';
return view('restaurant.categoryAdd')->with('myvar', $variable);
}
In your view (if HTML, simply):
<?php
print $myvar;
?>
The problem was in camelcase. I changed the view to 'categoryadd' and It's working now.

Variable undefined error in laravel blade view

Hey as i am passing a blade view which is having it own controller also i am including it into the view which does not have its own controller. it gives me an undefined variable error can any one help me how to it.
I have a view which does not have any controller only have Route like this Route::get('index', function () { return view('index'); }); in this view i am passing another view which having its own controller and also having some data from an array. but after using this view inside the view i get undefined variable error.
Two steps :
Declare & transfer $variable to View from Controller function.
public function index()
{
return view("index", [ "variable" => $variable ]);
}
Indicate where transferred $variable from Controller appear in view.blade.php.
{{ $variable }}
If you do not make sure, $variable is transferred or not
{{ isset($variable) ? $variable : '' }}
If this helps anyone, I was completely ignorant to the fact that my route was not hooked with the corresponding controller function and was returning the view directly instead, thereby causing this issue. Spent a good half hour banging my head till I realized the blunder.
Edit
Here again to highlight another blunder. Make sure you're passing your array correctly. I was doing ['key', 'value] instead of ['key' => 'value'] and getting this problem.
You can try this:
public function indexYourViews()
{
$test = "Test Views";
$secondViews = view('second',compact('test'));
return view('firstview',compact('secondViews'));
}
and after declare {{$secondViews}} in your main view file(firstview).
Hope this helps you.
public function returnTwoViews() {
$variable = 'foo bar';
$innerView = view('inner.view', ['variable' => $variable]);
return view('wrapper.view, ['innerView' => $innerView]);
}
This may be what you are looking for?
... inside your wrapper.view template:
{!! $innerView !!}
EDIT: to answer the question in the comment: In order to fetch each line you for do this inside your $innerView view:
#foreach($variable as $item)
{{ $item }}
#endforeach
... and in the wrapper view it will still be {!! $innerView !!}

Laravel 4: if statement in blade layout works strange

Could someone explain me why I get blank screen with printed string "#extends('layouts.default')" if I request page normally (not ajax)?
#if(!Request::ajax())
#extends('layouts.default')
#section('content')
#endif
Test
#if(!Request::ajax())
#stop
#endif
I'm trying to solve problem with Ajax, I don't want to create 2 templates for each request type and also I do want to use blade templates, so using controller layouts doesn't work for me. How can I do it in blade template? I was looking at this Laravel: how to render only one section of a template?
By the way. If I request it with ajax it works like it should.
Yes #extends has to be on line 1.
And I found solution for PJAX. At the beginning I was not sure this could solve my problem but it did. Don't know why I was afraid to lose blade functionality if you actually can't lose it this way. If someone is using PJAX and needs to use one template with and without layout this could be your solution:
protected $layout = 'layouts.default';
public function index()
{
if(Request::header('X-PJAX'))
{
return $view = View::make('home.index')
->with('title', 'index');
}
else
{
$this->layout->title = 'index';
$this->layout->content = View::make('home.index');
}
}
Try moving #extends to line 1 and you will see the blade template will render properly.
As for solving the ajax problem, I think it's better if you move the logic back to your controller.
Example:
…
if ( Request::ajax() )
{
return Response::eloquent($books);
} else {
return View::make('book.index')->with('books', $books);
}
…
Take a look at this thread for more info: http://forums.laravel.io/viewtopic.php?id=2508
You can still run your condition short handed in the fist line like so
#extends((Request::ajax())?"layout1":"layout2")

how to load view into another view codeigniter 2.1?

Ive been working with CI and I saw on the website of CI you can load a view as a variable part of the data you send to the "main" view, so, according the site (that says a lot of things, and many are not like they say ...ej pagination and others) i did something like this
$data['menu'] = $this->load->view('menu');
$this->load->view ('home',data);
the result of this is that I get an echo of the menu in the top of the site (before starts my body and all) and where should be its nothing, like if were printed before everything... I have no idea honestly of this problem, did anybody had the same problem before?
Two ways of doing this:
Load it in advance (like you're doing) and pass to the other view
<?php
// the "TRUE" argument tells it to return the content, rather than display it immediately
$data['menu'] = $this->load->view('menu', NULL, TRUE);
$this->load->view ('home', $data);
Load a view "from within" a view:
<?php
// put this in the controller
$this->load->view('home');
// put this in /application/views/home.php
$this->view('menu');
echo 'Other home content';
Create a helper function
function loadView($view,$data = null){
$CI = get_instance();
return $CI->load->view($view,$data);
}
Load the helper in the controller, then use the function in your view to load another one.
<?php
...
echo loadView('secondView',$data); // $data array
...
?>

Resources