0
我需要使用包含應用程序的所有類別及其各自子類別的導航菜單呈現側邊欄。 但是,當前頁面引用類別或子類別時,當前類別需要是菜單中的第一個,但我不知道如何正確執行此操作。在內部菜單中,將頂部的當前類別渲染到頂部
我首先想到的是重複循環兩次,在第一個循環中檢查相關類別是否與當前請求的類別相同,然後渲染它,否則跳過循環。 其他循環幾乎是相同的,但如果類別與當前請求的類別相同,則跳過循環到下一個元素。但這是一個非常糟糕的想法,重複兩次HTML,使維護成爲頭痛。
我當前的代碼:
//View Helper
<?php
namespace App\View\Helper;
class Category extends AbstractHelper {
protected $category;
/**
* @param \Entities\Product\Category $category
* @return \App\View\Helper\Category
*/
public function category(\Entities\Product\Category $category = null)
{
$this->category = $category;
return $this;
}
/**
* @return string
*/
public function renderSidebar()
{
$repositoryHelper = \Zend_Controller_Action_HelperBroker::getStaticHelper('repository');
$categories = $repositoryHelper->getRepository('Product\Category')->getCategoriesWithSubCategories();
$isCorrectAction = ($this->getRequestVariable('action', false) === 'products');
$isACategoryPage = false;
$requestedCategoryId = $this->getRequestVariable('category', false);
if($isCorrectAction && $requestedCategoryId){
$isACategoryPage = true;
}
return $this->view->partial(
'partials/categoriesSidebar.phtml',
array(
'categories' => $categories,
'isACategoryPage' => $isACategoryPage,
'requestedCategoryId' => (int) $requestedCategoryId
)
);
}
}
//inside categoriesSidebar.phtml
<ul class="sidebar-menu">
<?php foreach($this->categories as $category): ?>
<?php if($this->isACategoryPage && $this->requestedCategoryId === $category->getId()): ?>
//???
<?php endif; ?>
<li class="category" id="category-<?= $category->getId() ?>">
<a href="..." class="category-link"><?= $category->getName() ?></a>
<?php if($category->hasSubCategories()): ?>
<span class="subcategory-view desactivated"></span>
<ul class="category-subcategories">
<?php foreach($category->getSubCategories() as $subCategory): ?>
<li class="subcategory category-<?= $category->getId() ?>-subcategories" id="subcategory-<?= $subCategory->getId() ?>" data-category="<?= $category->getId() ?>">
<a href="..." class="subcategory-link"><?= $subCategory->getName() ?></a>
</li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
</li>
<?php endforeach; ?>
</ul>
你對我怎麼能做到這一點的想法?我沒有使用Zend_Navigation,在這種情況下,我應該使用它?或者我應該只使用CSS?