アプリケーションのすべてのカテゴリとそれぞれのサブカテゴリを含むナビゲーションメニューを備えたサイドバーをレンダリングする必要があります。ただし、現在のページがカテゴリまたはサブカテゴリを参照している場合、現在のカテゴリがメニューの最初である必要がありますが、これを正しく行う方法がわかりません。
最初に頭に浮かんだのは、ループを2回繰り返すことでした。最初のループでは、問題のカテゴリが現在のリクエストのカテゴリと同じかどうかを確認し、それをレンダリングします。そうでない場合は、ループをスキップします。他のループはほとんど同じですが、カテゴリが現在のリクエストのカテゴリと同じである場合は、ループをスキップして次の要素に進みます。しかし、これは非常に悪い考えです。HTMLを2回繰り返すと、メンテナンスが頭痛の種になります。
私の現在のコード:
//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だけでこれを作成する必要がありますか?