2012-02-09 3 views
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로 이것을해야합니까?

답변

1
//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> 
관련 문제