Malvolio
Malvolio

Reputation: 376

Laravel 4. Passing parameter to partial

I am making a sales support system with Laravel 4. I have a form for recording projects and another for saving sales notes. There I want to enable users to add a new project without having to leave the create sales note form for the new project form. I have a partial for the create project form, which I use in both the normal way of creating a project and the one I use in the create sales note. In the latter case I need to display a cancel button in addition to the submit button, so I intended to pass a parameter to my form partial to tell wether the cancel button is needed or not, like so:

projects/create.blade.php:

@include("partials.NewProjectForm", array("cancel" => "no"))

salesnotes/create.blade.php:

@include("partials.NewProjectForm", array("cancel" => "yes"))

partials/NewProjectForm.blade.php:

@if($cancel == "yes")
  {{ Form::button("Cancel") }}
@endif

In the "no" case the partial receives the passed variable as expected, in the "yes" case however it throws the "Undefined variable: cancel" error. I have tried as well including the partial with View::make()->with(), with the same result. Whats going wrong there?

Thanks.

Upvotes: 2

Views: 4690

Answers (2)

perNalin
perNalin

Reputation: 161

Your code is fine... That error could have been caused by a different call to the partial which had 'cancel' misspelled or missing. Perhaps the wrong 'salesnotes/create.blade.php' was deployed, or the 'salesnote' route directed to a different page which included the partial without setting the 'cancel' variable?

BTW - this doesn't solve your problem - but If you're creating a generic partial, suggest adding a check for isset($cancel) before checking for $cancel's value.

Upvotes: 0

lostphilosopher
lostphilosopher

Reputation: 4511

This answer ends up mentioning what you need. The trick is to use a different approach to rendering your partial.

Assuming you've got a partials directory under views that contains a file called header.blade.php add the following line the to view script you want to render the partial from:

{{ View::make('partials.blog.header', array('title' => 'test'))  }}

If you put the following line in header.blade.php you'll see that 'test' has been passed to your partial, and your partial has been rendered.

{{ $title }}

Hope that helps! (I was confused about this too when I first ran across it...)

Upvotes: 2

Related Questions