Blade Recursive,增加填充深度
Blade Recursive, increase the depth for padding
每次 children 在项目中时,我都想增加深度 int
,这样我就可以填充 a
标签。
下面是我的代码,我认为这会起作用,但是即使我在 $items
.
中的其中一项中得到了 children,数字仍然是 1
sidebar.blade.php
<nav class="flex flex-col mt-10">
<ul class="pl-4">
@foreach($items as $item)
<x-layouts.sidebar.items :item="$item"></x-layouts.sidebar.items>
@endforeach
</ul>
</nav>
sidebar.items.blade.php
<li>
@if(count($item) > 0)
<a href="{{ $item['href'] }}" class="focus:text-blue-500 dark-focus:text-blue-400 focus:outline-none w-full transition duration-500 ease-in-out pl-4 py-2 text-gray-700 dark:text-gray-400 hover:bg-blue-200 dark-hover:bg-blue-500 transition duration-500 ease-in-out block">
{{ $item['text'] }} {{ $depth }}
</a>
@if (count($item['children']) > 0)
<ul class="pl-4">
@foreach($item['children'] as $child)
<x-layouts.sidebar.items :item="$child" :depth="$loop->parent->index"></x-layouts.sidebar.items>
@endforeach
</ul>
@endif
@else
<hr>
@endif
</li>
Sidebar\Items.php
<?php
namespace App\View\Components\Layouts\Sidebar;
use Illuminate\View\Component;
use Illuminate\View\View;
class Items extends Component
{
/**
* @var array
*/
public $item;
/**
* @var int
*/
public $depth;
/**
* Create a new component instance.
*
* @param array $item
* @param int $depth
*/
public function __construct($item = [], $depth = 1)
{
$this->item = $item;
$this->depth += $depth;
}
/**
* Get the view / contents that represent the component.
*
* @return View|string
*/
public function render()
{
return view('components.layouts.sidebar.items');
}
}
数据:
$this->items = [
[ 'href' => '/home', 'text' => 'Home', 'children' => [], 'active' => 'border-l-2 border-blue-500' ],
[ 'href' => 'javascript:void(0)', 'text' => 'Test', 'children' => [], 'active' => '' ],
[ 'href' => 'javascript:void(0)', 'text' => 'Websites', 'children' => [], 'active' => '' ],
[ 'href' => 'javascript:void(0)', 'text' => 'Websites', 'children' => [], 'active' => '' ],
[],
[
'href' => '/administration',
'text' => 'Administration',
'children' => [
[
'href' => 'javascript:void(0)',
'text' => 'Locked Devsites',
'active' => '',
'children' => []
]
],
'active' => ''
],
[ 'href' => 'javascript:void(0)', 'text' => 'Documentation', 'children' => [], 'active' => '' ],
[ 'href' => 'javascript:void(0)', 'text' => 'Logout', 'children' => [], 'active' => '' ]
];
结果:
Home 1
Test 1
Websites 1
Websites 1
Administration 1
Locked Devsites 5
Documentation 1
Logout 1
如果我说对了你想要实现的目标,你甚至不需要 $depth
属性,因为 blade 的 @foreach
指令有它自己的深度 属性 这告诉你嵌套的程度。
在您的代码中,改为使用:
:depth="$depth"
使用:
:depth="$loop->depth"
每次 children 在项目中时,我都想增加深度 int
,这样我就可以填充 a
标签。
下面是我的代码,我认为这会起作用,但是即使我在 $items
.
sidebar.blade.php
<nav class="flex flex-col mt-10">
<ul class="pl-4">
@foreach($items as $item)
<x-layouts.sidebar.items :item="$item"></x-layouts.sidebar.items>
@endforeach
</ul>
</nav>
sidebar.items.blade.php
<li>
@if(count($item) > 0)
<a href="{{ $item['href'] }}" class="focus:text-blue-500 dark-focus:text-blue-400 focus:outline-none w-full transition duration-500 ease-in-out pl-4 py-2 text-gray-700 dark:text-gray-400 hover:bg-blue-200 dark-hover:bg-blue-500 transition duration-500 ease-in-out block">
{{ $item['text'] }} {{ $depth }}
</a>
@if (count($item['children']) > 0)
<ul class="pl-4">
@foreach($item['children'] as $child)
<x-layouts.sidebar.items :item="$child" :depth="$loop->parent->index"></x-layouts.sidebar.items>
@endforeach
</ul>
@endif
@else
<hr>
@endif
</li>
Sidebar\Items.php
<?php
namespace App\View\Components\Layouts\Sidebar;
use Illuminate\View\Component;
use Illuminate\View\View;
class Items extends Component
{
/**
* @var array
*/
public $item;
/**
* @var int
*/
public $depth;
/**
* Create a new component instance.
*
* @param array $item
* @param int $depth
*/
public function __construct($item = [], $depth = 1)
{
$this->item = $item;
$this->depth += $depth;
}
/**
* Get the view / contents that represent the component.
*
* @return View|string
*/
public function render()
{
return view('components.layouts.sidebar.items');
}
}
数据:
$this->items = [
[ 'href' => '/home', 'text' => 'Home', 'children' => [], 'active' => 'border-l-2 border-blue-500' ],
[ 'href' => 'javascript:void(0)', 'text' => 'Test', 'children' => [], 'active' => '' ],
[ 'href' => 'javascript:void(0)', 'text' => 'Websites', 'children' => [], 'active' => '' ],
[ 'href' => 'javascript:void(0)', 'text' => 'Websites', 'children' => [], 'active' => '' ],
[],
[
'href' => '/administration',
'text' => 'Administration',
'children' => [
[
'href' => 'javascript:void(0)',
'text' => 'Locked Devsites',
'active' => '',
'children' => []
]
],
'active' => ''
],
[ 'href' => 'javascript:void(0)', 'text' => 'Documentation', 'children' => [], 'active' => '' ],
[ 'href' => 'javascript:void(0)', 'text' => 'Logout', 'children' => [], 'active' => '' ]
];
结果:
Home 1
Test 1
Websites 1
Websites 1
Administration 1
Locked Devsites 5
Documentation 1
Logout 1
如果我说对了你想要实现的目标,你甚至不需要 $depth
属性,因为 blade 的 @foreach
指令有它自己的深度 属性 这告诉你嵌套的程度。
在您的代码中,改为使用:
:depth="$depth"
使用:
:depth="$loop->depth"