yii有兩種Breadcrumbs寫法,
one:
echo Breadcrumbs::widget([ 'itemTemplate' => "<li><i>{link}</i></li>\n", // template for all links 'links' => [ [ 'label' => 'Post Category', 'url' => ['post-category/view', 'id' => 10], 'template' => "<li><b>{link}</b></li>\n", // template for this link only ], ['label' => 'Sample Post', 'url' => ['post/edit', 'id' => 1]], 'Edit', ], ]);
two:
echo Breadcrumbs::widget([ 'links' => isset($this->params['breadcrumbs']) ? $this->params['breadcrumbs'] : [], ]);
第二種在yii的main.php中用實例,就是最簡單home->edit;
今天將要說的第一種,
echo Breadcrumbs::widget([ 'itemTemplate' => "<li><i>{link}</i></li>\n", // template for all links 'links' => $link, ]);
由於此處需要使用breadcrumbs展現相應的tree結構,經過對link中的數據進行重組可以將程序寫成上面的樣子,
if (!empty($title)) { $title = $title; $title_arr = explode('/', $title); foreach ($title_arr as $k => $item) { if (!empty($item)) { $link[] = ['label' => $item, 'url' => ['/tree/edit/', 'id' => $item, 'type' => 1,]]; } } $title_s = ''; foreach ($link as $k => $item) { if ($k !== 0) { if ($k === 1) { $title_s = $link[$k - 1]['label']; } else { $title_s = $title_s . '/' . $link[$k - 1]['label']; } $link[$k] = ['label' => $item['label'], 'url' => ['/tree/edit/', 'id' => $item['label'], 'type' => 1, 'title' => $title_s]]; } else { $title_s = $item['label']; } } $title = $title . '/' . $node_id['nodeId']; } else { $title = $node_id['nodeId']; } $link[] = $node_id['nodeId'];
這樣就展示了一個tree的結構