spotonlive/sl-navigation

View on GitHub
docs/04. Render.md

Summary

Maintainability
Test Coverage
# Render
How to render your menu.

## Depth
You can choose the rendering depth by:

```php
// Render all depths
echo $container->render();

// Render only first depth
echo $container->render(0);

// Render n depth
echo $container->render(n);
```

## How to access the service

### Dependency injection

```php
<?php

namespace App\Controllers;

use SpotOnLive\Navigation\Services\NavigationService;
use SpotOnLive\Navigation\Services\NavigationServiceInterface;

class Controller
{
    /** @var NavigationServiceInterface */
    protected $navigationService;

    public function __construct(NavigationService $navigationService)
    {
        $this->navigationService = $navigationService;
    }

    public function action()
    {
        $navigationContainer = $this->navigationService->getContainer('admin.menu');
    }
}
```

## Display methods
You can both render the menu using the standard renderer or use a partial view

### Render

**Dependency example**
```php
$container = $navigationService->getContainer('admin.menu');
echo $container->render();
```

**Blade example**
```php
{!! NavigationHelper::getContainer('admin.menu')->render() !!}
```

### Partial render
Render using a partial

**Dependency example**
```php
$container = $navigationService->getContainer('admin.menu');
echo $container->renderPartial('blender.view');
```

**Blade example**
```php
{!! NavigationHelper::getContainer('admin.menu')->renderPartial('blender.view') !!}
```

This will pass the `SpotOnLive\Navigation\Navigation\ContainerInterface` as **$container** to the view.

`menu.blade.php`
```php
<ul>
@foreach($container->getPages() as $page)
    <li>
        @if($page->isActive())
            <span class="fa fa-caret-right"></span>
        @endif

        <a href="{{$page->getUrl()}}">
            {!! $page->getLabel() !!}
        </a>
    </li>
@endforeach
</ul>
```
* Go back to [**Containers**](02. Containers.md)