Muhaimin
Muhaimin

Reputation: 1643

Passing additional variable to partial using @each in blade

From the documentation, only 4 parameters can be passed to @each. I don't think that using @include will help. Code is below

@each('partials.nav.categories', $groupCategories, 'parent')

I need to send through an additional variable for use in the partial partials.nav.categories. This variable is not contained within the $groupCategories array.

Is there any way to send this additional variable through or do I have to append it to each item in the partials.nav.categories array?

Thanks

Upvotes: 20

Views: 6632

Answers (3)

Ravi Hirani
Ravi Hirani

Reputation: 6539

I think you are right. Append data to $groupCategories is the right way. As per documentation, The fourth param is what will be displayed if the $groupCategories is empty. You can either pass a view template, which will be shown only once, or any text prepended with raw| will be displayed as is.

General format:

@each('viewfile-to-render', $data, 'variablename','optional-empty-viewfile')

The first argument is the template to render. This will usually be a partial, like your nameofyourblade.blade.php.

The second one is the iterable dataset, in your case $groupCategories.

The Third is the variable name the elements will use when being iterated upon. For example, in foreach ($data as $element), this argument would be element (without the $).

The fourth argument is an optional one – it’s the name of the template file which should be rendered when the second argument ($data) is empty, i.e. has nothing to iterate over. If we apply all this to our case, we can replace this entire block:

@if (count($groupCategories) > 0)
    <ul>
    @foreach ($groupCategories as $parent)
        @include('partials.nav.categories', $parent)
    @endforeach
    </ul>
@else
    @include('partials.group-none')
@endif

with

@each('partials.nav.categories', $groupCategories, 'parent', 'partials.group-none')

Upvotes: 1

Snapey
Snapey

Reputation: 4110

You only need to pass to the partial instance, the variables that change on each use of the partial.

The code inside the partial itself can access anything that is in scope of the parent.

So, if for instance, the main view can use $user, so can the partial - on the understanding that it will be the same value on each inclusion of the partial.

Not sure if this helps since you don't say if the additional parameter needs to be different for each inclusion of the partial.

Upvotes: 0

xAoc
xAoc

Reputation: 3608

You can share variable from your controller view()->share('key', 'value'); That's value will be available across all of your views.

Or, you can create view composers exactly for this view.

public function yourmethod()
{
   view()->composer('partials.nav.categories', function($view) {
      $view->with('var', 'value');
   });

   return view('path.to.view', ['groupCategories' => $categories]);
}

And $var will be available only in partials.nav.categories view.

Upvotes: 1

Related Questions