lavary/laravel-menu

How to call link->attr target on blade?

charmtorio opened this issue · 2 comments

Hi,

I'm trying to call the "_blank" on my laravel blade file:

Here's my Menu structure:

$menu->add('Menu',
     ['url'    => 'www.google.com',
       'id'     => 2
     ])->link->attr(['target' => $menu['new_window'] ? '_blank' : '' ]);

How can I call and set the <a> to have <a target="_blank">?

Here's my Blade file:

<a @lm_attrs($item->link) @lm_endattrs class="nav-link" href="{!! $item->url() !!}">{!! $item->title !!}}</a>

I could do the below, but maybe there's something better?

<a @if($item->link->attr('target')) target="_blank" @endif class="nav-link" href="{!! $item->url() !!}">{!! $item->title !!}}</a>

Thank you.

I also have a submenu on my blade...

@if($item->hasChildren())
<ul class="dropdown-menu">
     @include(config('laravel-menu.views.bootstrap-items'), array('items' => $item->children()))
</ul> 
@endif

How can add the target="_blank" on the @include line?

I think what you have should work.

<a @lm_attrs($item->link) @lm_endattrs class="nav-link" href="{!! $item->url() !!}">{!! $item->title !!}}</a>

Looking at the first block of code you have, your Menu structure. Did you try adjusting the $menu->add to always include 'target' => '_blank'? Maybe $menu['new_window'] is false or a falsey value. Like...

$menu->add('Menu',
     ['url'    => 'www.google.com',
       'id'     => 2
     ])->link->attr(['target' => '_blank' ]);

To see if that will create the attribute you are looking for?

--

For your second question, the $item->children() will pass the attributes with those child items. You could also add another value in the array and pass the link attribute directly. Like...

@if($item->hasChildren())
<ul class="dropdown-menu">
     @include(config('laravel-menu.views.bootstrap-items'), array('original' => $item, 'items' => $item->children()))
</ul> 
@endif

Then within the sub-view, you would have access to $original->link for the parent item.