Skip to content
niknetniko edited this page Mar 5, 2016 · 4 revisions

Attaching Attributes

Attaching attributes to your menu items is simple. You are able to define as many attributes as you may need to any item through a variety of consistent interfaces - utilize whichever one you think is the cleanest.

Pass attributes through the add() method

Menu::make('Example', function(Builder $menu) {
    $menu->add('Link One', ['url' => 'link-one', 'class' => 'my-super-class', 'id' => 'link-one']);
    $menu->add('Link Two', ['url' => 'link-two', 'class' => 'my-super-class', 'id' => 'link-two']);
});

In this example, both the class and id attributes will be attributed to each item.

Attach attributes via the attribute() method

You may also make use of the attribute() method to assign attributes rather than passing them through the add() method. This results is easier to read code:

Pass as an array

Menu::make('Example', function(Builder $menu) {
    $menu->add('Link One', 'link-one')->attribute(['class' => 'my-super-class', 'id' => 'link-one']);
    $menu->add('Link Two', 'link-two')->attribute(['class' => 'my-super-class', 'id' => 'link-two']);
});

Pass individually

Menu::make('Example', function(Builder $menu) {
    $menu->add('Link One', 'link-one')
        ->attribute('class', 'my-super-class')
        ->attribute('id', 'link-one');

    $menu->add('Link Two', 'link-two')
        ->attribute('class', 'my-super-class')
        ->attribute('id', 'link-two');
});

Rendering Attributes

Rendering attributes is simple within your view file. Simply echo out the item's attributes() method as a raw string, and you'll be good to go! Caffeinated Menus will automatically generate the correct HTML syntax.

Blade

{!! $item->attributes() !!}

Twig

{{ item.attributes()|raw }}