0
アプリケーションのすべてのカテゴリとそれぞれのサブカテゴリを含むナビゲーションメニューを使用してサイドバーをレンダリングする必要があります。 しかし、現在のページがカテゴリまたはサブカテゴリを参照しているときは、現在のカテゴリがメニューの最初のものである必要がありますが、これを正しく行う方法はわかりません。メニュー内で現在のカテゴリを上にレンダリング
最初のことは、問題のカテゴリが現在のリクエストのカテゴリと同じであるかどうかを最初のループチェックで2回繰り返すことでした。それ以外の場合はループをスキップします。 もう1つのループはほぼ同じですが、カテゴリが現在の要求のカテゴリと同じ場合は、そのループを次の要素にスキップしてください。しかし、これは非常に悪い考えです.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でこれを作るべきですか?