Sage 10 render from View Composer

Hi,
I have a test code in App Composer:

use function Roots\view;
...

public function with()
{
    return [
        ...
        'list' => $this->list(),
    ];
}

public function list()
{
    return view('components.some-list', [
        'users' => ['Stipe', 'Frane', 'Mate'],
    ])->render();
}

And in some-list.blade.php

<ul>
    @foreach($users as $user)
        <li>{{ $user }}</li>
    @endforeach
</ul>

but when I call it in page template with {!! $list !!} I get exhausted memory.

In Sage 9 this was easy:

$content .= template('components.blog-card', [
  'list' => $list
]);

I think your approach here is flawed.

First, list() is a protected keyword. Don’t use that as the name of your function definition.

Second, there are better ways to render a component into your view. Rather than returning it from a Composer, you can render the component using the Blade Component Template Tag. Using template tags is a cleaner solution and easier to maintain than having your view render from inside of the App composer. You can use wp acorn make:component UserList to scaffold out the boilerplate component files.

This topic was automatically closed after 42 days. New replies are no longer allowed.