How to determine if the `navList` menu item has child navigation (`item.NavList`) and perform nested rendering?

Calendar 👁️ 68

Secure CMS navigation menu: how to usenavListImplement multi-level nesting and sub-navigation judgment

As an experienced website operations expert, I fully understand the importance of a clear and flexible navigation system for user experience and the overall architecture of the website. In AnQiCMS (AnQiCMS),navListThe label is the core tool we use to build a powerful navigation system.It not only helps us easily display the main navigation of the website, but also provides a powerful mechanism to judge whether the menu item has a sub-navigation and performs elegant nested rendering, thus building a multi-level menu with clear hierarchy and rich functions.

Today, let's delve into how to巧妙ly use in AnQi CMSnavListtags, judge and render menu items with sub-navigation

Get to knownavList: The cornerstone of navigation in AnQi CMS

In the template design of AnQi CMS,navListThe tag bears the important responsibility of obtaining the website navigation list. It allows us to navigate according to different needs, such as the main navigation, footer navigation, or sidebar navigation, throughtypeIdParameters are used to call predefined navigation categories. When we configure the menu structure in the background "Website Navigation Settings", including the first-level menu and possibly existing second-level and even more sub-menus,navListThe tag will extract these structured navigation data for use in the front-end template.

Every one fromnavListThe menu items looped out by the tag are wrapped in oneitemIn the variable. ThisitemContained navigation title(Title), Link(Link), Subtitle(SubTitle), Description(Description) etc. Basic information. The key to determining whether a menu item has sub-navigation is to check its special property: NavList.

Core judgment:item.NavListThe mystery of

item.NavListIt is a very critical attribute, if there are sub-menus under the current menu item, thenitem.NavListit will be an array containing these submenu items. More cleverly, thisNavListeach submenu item in the array also has the same field structure as the parentitemincluding its own.NavListAttribute. This recursive structure is the foundation for implementing multi-level nested navigation.

To determine whether a menu item has child navigation, we can simply use the Anqi CMS template engine.ifConditional judgment tag, checkitem.NavListIt exists or is not empty

For example, in a typical two-level navigation structure, we can write the template code like this:

{% navList navs %}
    <ul class="main-nav">
        {%- for item in navs %} {# 遍历一级导航 #}
            <li class="{% if item.IsCurrent %}active{% endif %}">
                <a href="{{ item.Link }}">{{item.Title}}</a>
                {%- if item.NavList %} {# <-- 关键判断点:如果存在子导航 #}
                <dl class="sub-nav">
                    {%- for inner in item.NavList %} {# <-- 嵌套渲染子导航 #}
                        <dd class="{% if inner.IsCurrent %}active{% endif %}">
                            <a href="{{ inner.Link }}">{{inner.Title}}</a>
                        </dd>
                    {% endfor %}
                </dl>
                {% endif %}
            </li>
        {% endfor %}
    </ul>
{% endnavList %}

In this code, we first use{% navList navs %}Get the top-level navigation list and iterate overforeach item. Inside the loop,{%- if item.NavList %}this line of code is used to judge the currentitemIs the core of having child navigation. Once it is judged to be true, we will enter a newdltag inside, and use anotherforloop({%- for inner in item.NavList %}) to render these child navigation items.innerA variable represents each sub-navigation item, it also hasTitle/Linkproperties that are consistent with the parent levelitem. The usage method is consistent.

Advanced Application: Dynamic Content and Multi-level Nesting

This kind of judgment and nested pattern is not limited to two levels, it has a strong recursiveness. If your navigation has three levels or even more, you caninnercheck again inside the loopinner.NavListRepeat the above nested rendering logic.

What is more important, navigation menu items can not only link to ordinary pages or categories, but we can also use them to dynamically display content related to the navigation item, such as specific product lists under a product category, or subcategory lists under a category. This usually requires combininginner.PageIdTo complete the attribute.inner.PageIdRecords the ID of the page, category, or model associated with the navigation item.

For example, in a complex navigation menu, you may want to display not only the subcategories of a product category when the mouse hovers over it, but also some popular products directly.

This is an example of displaying related products under a navigation item:

<ul>
    {% navList navList with typeId=1 %}
    {%- for item in navList %}
    <li>
        <a href="{{ item.Link }}">{{item.Title}}</a>
        {%- if item.NavList %} {# 如果有一级子导航 #}
        <ul class="nav-menu-child">
            {%- for inner in item.NavList %} {# 遍历一级子导航 #}
            <li>
                <a href="{{ inner.Link }}">{{inner.Title}}</a>
                {% archiveList products with type="list" categoryId=inner.PageId limit="8" %} {# <-- 这里根据子导航关联的分类ID调用产品列表 #}
                {% if products %} {# 如果有产品内容 #}
                <ul class="nav-menu-child-child">
                    {% for product in products %}
                    <li><a href="{{product.Link}}">{{product.Title}}</a></li>
                    {% endfor %}
                </ul>
                {% endif %}
                {% endarchiveList %}
            </li>
            {% endfor %}
        </ul>
        {% endif %}
    </li>
    {% endfor %}
    {% endnavList %}
</ul>

Here, we useinner.PageIdto

Related articles

How to highlight the current page in the `navList` navigation menu based on the `IsCurrent` property?

In the fast-paced digital world, a clear and intuitive website navigation menu is the foundation of a good user experience.As an experienced website operations expert, I am well aware of AnQiCMS's powerful capabilities in providing efficient and customizable content management solutions.It not only performs excellently but also considers the operator's needs in detail, for example, the built-in `navList` tag combined with the `IsCurrent` attribute can easily highlight the current page, greatly enhancing the professionalism and user-friendliness of the website.

2025-11-06

How to judge whether a contact field (such as Qrcode) in the AnQiCMS template has a value?

In website operation and template development, we often need to dynamically adjust the display of the front-end page based on the setting of the background content.Especially for important information such as contact information, ensure that it is displayed after it exists, which can effectively avoid broken links, blank areas, or incomplete information on the page, thereby improving the user experience.Today, let's delve into how to elegantly and accurately determine whether a contact field (such as `Qrcode`) obtained by the `contact` tag in the AnQiCMS template has been set.

2025-11-06

How to judge whether the website is closed and display a prompt based on the `SiteCloseTips` of the system settings (`{% system %}`)?

As an experienced website operations expert, I know that the stable operation of the website is of great importance, but sometimes, due to reasons such as maintenance, upgrades, or data migration, the website has to temporarily shut its doors.How to communicate with visitors elegantly during a closure period, ensuring user experience while effectively managing expectations, is a detail that every operator needs to pay attention to.AnQiCMS (AnQiCMS) provides a flexible mechanism in this regard, allowing us to deal with it easily.Today, let's delve deeply into how to set up tags in Anqi CMS through system settings `{% system

2025-11-06

How to determine if there is a previous or next document on the document detail page and display navigation links?

As an experienced website operations expert, I know that a smooth reading experience is crucial when users browse content.A good document detail page should not only display the core content but also guide users to the next step, such as reading related content or easily switching to the previous or next page.This not only increases the user's stay time on the website and reduces the bounce rate, but it is also SEO-friendly, helping search engines to better crawl and understand the website structure.In AnQiCMS (AnQiCMS), implement the previous and next navigation links on the document detail page

2025-11-06

How to determine if the `linkList` friendship link tag list is empty to avoid displaying an empty tag?

As an experienced website operations expert, I have accumulated rich experience in content management and template optimization in AnQiCMS.I know well that a smooth, beautiful and quick-loading website cannot be separated from the meticulous refinement of every detail.Today, let's delve deeply into an issue that is often overlooked in template development but is crucial for user experience and page neatness: how to avoid displaying redundant empty tags when the `linkList` friendship link tag list is empty.Avoid displaying empty tags: AnQi CMS

2025-11-06

How to conditionally output the standard link according to the `CanonicalUrl` tag in AnQiCMS templates?

Hello! As an experienced website operations expert, I fully understand that in daily work, we not only need to pay attention to the quality of the website content, but also to the impact of technical details on SEO.The canonical URL is one of the crucial details that can effectively solve the problem of duplicate website content, concentrate page authority, and thus improve search engine rankings.Today, let's delve deeply into how to intelligently and elegantly output standard links based on the existence or absence of the `CanonicalUrl` field within the `tdk` tag in AnQiCMS templates

2025-11-06

In the `pagination` tag, how to determine if the current page is the first or last page to control the style?

As an experienced website operations expert, I know the importance of a powerful and flexible content management system for the success of a website.AnQiCMS leverages the efficiency of the Go language and its deep consideration for content operation, providing us with many conveniences.Today, let's delve into the wonderful use of the `pagination` tag in AnQiCMS, especially how to accurately judge whether the current page is the first page or the last page, thereby flexibly controlling the style to create a smoother and more intuitive navigation experience for users.

2025-11-06

How to conditionally display the 'Previous' and 'Next' buttons in the `pagination`?

As an experienced website operation expert, I am willing to analyze the conditional display techniques of the pagination navigation buttons in AnQi CMS in detail.AnQi CMS is known for its efficiency and flexibility, its template engine provides powerful functions, allowing us to easily achieve both beautiful and practical page interactions.In website content management, pagination is an indispensable element, and how to intelligently display the "Previous Page" and "Next Page" buttons is directly related to the smoothness of the user's browsing experience.### SecureCMS

2025-11-06