Skip to content

Rendering the Sidebar

Patrick Brouwers edited this page Jun 20, 2015 · 2 revisions

The rendering of your sidebar can be done inside a View composer or View creator. You can use dependency injection to gain access to the Sidebar class and the SidebarRenderer. Simply assign the rendered result to a variable and echo that variable inside the view.

View creator:

use Maatwebsite\Sidebar\Presentation\SidebarRenderer;

class SidebarCreator
{
    /**
     * @var YourSidebar
     */
    protected $sidebar;

    /**
     * @var SidebarRenderer
     */
    protected $renderer;

    /**
     * @param YourSidebar $sidebar
     * @param SidebarRenderer       $renderer
     */
    public function __construct(YourSidebar $sidebar, SidebarRenderer $renderer)
    {
        $this->sidebar  = $sidebar;
        $this->renderer = $renderer;
    }

    /**
     * @param $view
     */
    public function create($view)
    {
        $view->sidebar = $this->renderer->render(
            $this->sidebar
        );
    }
}
View::creator(
    'layouts.partials.sidebar',
    'App\SidebarCreator'
);

sidebar.blade.php:

{!! $sidebar !!}