在菜单内,将当前类别呈现在顶部

发布于 2025-01-03 11:11:18 字数 3052 浏览 0 评论 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 来做这个?

I need to render a sidebar with a navigation menu that contains all the categories, and its respective subcategories, of an application.
However, when the current page is referring to a category or subcategory, the current category needs to be the first from the menu, but I have no idea how to do this correctly.

The first thing that crossed my mind was to repeat the loop twice, in the first loop check if the category in question is the same as the category of the current request, and then render it, otherwise skip the loop.
The other loop would be almost the same thing, but if the category is the same as the category of the current request, skip the loop to the next element. But this is a very bad idea, repeat the HTML twice, making maintenance a headache.

My current code:

//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>

Do you have ideas on how I could do this? I'm not using Zend_Navigation, In this case, I should be using it? Or should I make this just with CSS?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

沫离伤花 2025-01-10 11:11:18
//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>
//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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文