在内部菜单中,将顶部的当前类别渲染到顶部

问题描述:

我需要使用包含应用程序的所有类别及其各自子类别的导航菜单呈现侧边栏。 但是,当前页面引用类别或子类别时,当前类别需要是菜单中的第一个,但我不知道如何正确执行此操作。在内部菜单中,将顶部的当前类别渲染到顶部

我首先想到的是重复循环两次,在第一个循环中检查相关类别是否与当前请求的类别相同,然后渲染它,否则跳过循环。 其他循环几乎是相同的,但如果类别与当前请求的类别相同,则跳过循环到下一个元素。但这是一个非常糟糕的想法,重复两次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?

//inside categoriesSidebar.phtml 
<ul class="sidebar-menu"> 
    <?php ob_start(); ?> 
    <?php foreach($this->categories as $category): ?> 
     <?php if($this->isACategoryPage && $this->requestedCategoryId === $category->getId()): ?> 
      <?php $current = $this->partial(
           'partials/categoriesSidebarItem.phtml', 
           array(
            'category'  => $category, 
           )); ?> 
     <?php endif; ?> 
     <?php echo $this->partial(
         'partials/categoriesSidebarItem.phtml', 
         array(
          'category'  => $category, 
         )); ?> 
    <?php endforeach; ?> 
    <?php $list = ob_get_clean(); echo $current; echo $list; ?> 
</ul> 

// inside categoriesSidebarItem.phtml 
     <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>