Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to call link->attr target on blade? #290

Closed
charmtorio opened this issue Jan 28, 2022 · 2 comments
Closed

How to call link->attr target on blade? #290

charmtorio opened this issue Jan 28, 2022 · 2 comments
Labels

Comments

@charmtorio
Copy link

charmtorio commented Jan 28, 2022

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.

@charmtorio
Copy link
Author

charmtorio commented Jan 28, 2022

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?

@dustingraham
Copy link
Collaborator

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants