Reputation: 7225
How can I define a default value for a record in @include
? Here is my code:
// view - questions.create
@include('questions.__answer', ['number' => 0]) // <-- 'answer' => null by DEFAULT
// view - questions.edit
@include('questions.__answer', ['number' => $index, 'answer' => $answer])
As you see, I have create / edit views for a Question
model, which contains multiple possible answers. The html code for a single answer is located in a separate partial view to eliminate code duplication. I need to reuse this view both for answer creation, where new record should be created from scratch, and editing of an existing answer, whose fields should be populated with existing data.
I certainly could pass 'answer' => null
explicitly, but I prefer it to be passed implicitly.
Upvotes: 2
Views: 77
Reputation: 3288
I never found a great way to do this either, so I had to make a little hack.
https://github.com/laravel/framework/issues/1844
There are a lot of ways of setting variables, but pick your favorite. Mine was using a @set
.
Then, at the top of the include, do this.
@set('number', isset($number) ? $number : null);
Yeah, I know, it's gross. I never played around with View Composers, though, and you may be able to make use of this:
https://laravel.com/docs/5.1/views#view-composers
Theoretically, you could use view()
and a ViewComposer to render a partial template inline with Blade and tuck away all that nasty logic into OOP where it belongs.
Upvotes: 2