How to use the `navList` tag to create a navigation bar with submenus in the Anqi CMS template?

Calendar 👁️ 75

In the template development of AnQi CMS, building a functional and user-friendly navigation bar is one of the core tasks of website frontend development. Especially when the navigation needs to include multi-level sub-menus, AnQi CMS providesnavListTags can help us efficiently meet this need.

UnderstandingnavListThe core role of tags

navListThe tag is a tool in Anqi CMS template that is specifically used to retrieve website navigation data and display it on the page.It allows us to dynamically extract menu items from the pre-set navigation structure in the background, including top-level menus, second-level sub-menus, etc., greatly enhancing the flexibility and maintenance efficiency of the template.

The backend navigation settings of Anqi CMS are very flexible, you can create multiple navigation categories (such as "top navigationnavListThe tag's mission is to present these structured data on your website front-end template.

Its basic syntax structure is concise and clear:

{% navList 变量名称 %}
    {# 在这里通过循环处理导航数据 #}
{% endnavList %}

Among them,变量名称It is a variable you define yourself to storenavListData of all navigation tags obtained. Usually, we would name itnavsornavListso that it can be referenced in subsequent code.

Get to know in-depthnavListparameters

navListThe label supports several key parameters to help you accurately obtain the navigation data you need:

  • typeId: Navigation category IDThis is the most commonly used parameter. In the Anqi CMS backend navigation settings, you can create different categories for different navigation, each category has a unique ID. ThroughtypeIdParameter, you can specifynavListOnly get navigation data of a specific category. For example,typeId=1may indicate getting data for 'top navigation', whiletypeId=2Used to retrieve the data for the 'bottom navigation'. By default, if not specifiedtypeIdit will retrieve the navigation category with ID 1.

  • siteId: Site IDIf you are using the AnQi CMS multi-site management feature and want to call navigation data set by other sites in the current site template, you can usesiteIdThe parameter. However, for most single-site users, this parameter usually does not need to be manually set, as the system will automatically recognize the current site.

By combining these parameters, you can easily display different navigation menus in different areas of the website.

Navigation data structure analysis: Understanding how to build sub-menus

navListThe data obtained from the tag is an array containing multiple navigation items. In{% navList %}and{% endnavList %}you would usually usefora loop to iterate over these navigation items. Each item in the loopitem(Assuming you name the variablenavs, each navigation item isitem) has the following key properties, which are essential for building a submenu navigation bar:

  • Title:The display name of the navigation item.
  • SubTitle:Navigation item subtitle, if set in the background.
  • Description:Description information of the navigation item.
  • Link:URL link to jump to after clicking the navigation item.
  • PageId:If the navigation item links to a category or a single page, it will contain the corresponding ID. This is very useful when you need to retrieve content based on the navigation item ID.
  • IsCurrent:A boolean value indicating whether the current navigation item is the page the user is visiting. This is very important for implementing the highlight display of the navigation menu.
  • NavList: This is the key to implementing the submenu! NavListit is also an array that contains all the sub-navigation items of the current navigation item. This structure is recursive, meaning that sub-navigation items can also have their ownNavListThus, theoretically, it supports an infinite number of menu levels (although it is usually recommended not to exceed three levels in actual website design to ensure user experience).

Practice Exercise: Step by step to build a navigation bar with submenus

After understanding the data structure, we can start building a navigation bar with sub-menus in the template. The following are some common implementation methods.

1. Build the basic second-level navigation bar

This is the most common navigation structure, a first-level menu containing several second-level sub-menus.

{% navList navs with typeId=1 %} {# 假设1是您的顶部导航类别ID #}
<ul class="main-nav"> {# 主导航容器 #}
    {%- for item in navs %} {# 遍历一级导航项 #}
        <li class="nav-item {% if item.IsCurrent %}active{% endif %}">
            <a href="{{ item.Link }}" class="nav-link">{{ item.Title }}</a>
            {%- if item.NavList %} {# 判断当前一级导航项是否有子菜单 #}
            <ul class="sub-nav"> {# 子菜单容器 #}
                {%- for inner in item.NavList %} {# 遍历二级导航项 #}
                    <li class="sub-nav-item {% if inner.IsCurrent %}active{% endif %}">
                        <a href="{{ inner.Link }}" class="sub-nav-link">{{ inner.Title }}</a>
                    </li>
                {% endfor %}
            </ul>
            {% endif %}
        </li>
    {% endfor %}
</ul>
{% endnavList %}

Code explanation:

  • We first use{% navList navs with typeId=1 %}Get the navigation category data for ID 1.
  • The outermostforLoop throughnavsProcess each first-level navigation item.
  • item.IsCurrentTo determine if the current navigation item is active, if the user is visiting the page, then addactivethe class name to highlight the CSS style.
  • {%- if item.NavList %}It is a key judgment that checks whether the current level navigation item contains a submenu. Only whenNavListit is not empty, will the HTML structure of the submenu be rendered.
  • Inner layers.forLoop throughitem.NavListHandle each second-level navigation item, and also judgedinner.IsCurrent.

2. Display specific content in the submenu (such as product lists)

Sometimes, you may want to display a dropdown list containing the latest products or articles when hovering over a navigation item. This can be achieved by combiningnavListandarchiveListthe tag.

<ul class="main-nav">
    {% navList navList with typeId=1 %}
    {%- for item in navList %}
    <li class="nav-item">
        <a href="{{ item.Link }}" class="nav-link">{{item.Title}}</a>
        {%- if item.NavList %} {# 判断是否存在二级菜单 #}
        <ul class="sub-nav drop-menu">
            {%- for inner in item.NavList %} {# 遍历二级菜单项 #}
            <li class="sub-nav-item">
                <a href="{{ inner.Link }}" class="sub-nav-link">{{inner.Title}}</a>
                {% if inner.PageId > 0 %} {# 假设二级菜单链接的是某个分类(PageId即分类ID) #}
                    {# 在二级菜单下显示该分类的最新产品或文章,假设categoryId与inner.PageId对应 #}
                    {% archiveList products with type="list" categoryId=inner.PageId limit="8" %}
                    {% if products %}
                    <ul class="sub-sub-nav-content"> {# 展示内容的容器 #}
                        {% for product in products %}
                        <li><a href="{{product.Link}}">{{product.Title}}</a></li>
                        {% endfor %}
                    </ul>
                    {% endif %}
                    {% endarchiveList %}
                {% endif %}
            </li>
            {% endfor %}
        </ul>
        {% endif %}
    </li>
    {% endfor %}
    {% endnavList %}
</ul>

Code explanation:

  • Here we assume that the secondary menu itemPageIdstores a category ID
  • {% if inner.PageId > 0 %}determines whether the secondary menu item is associated with an actual category.
  • {% archiveList products with type="list" categoryId=inner.PageId limit="8" %}Tags are used to obtain the latest 8 product (or article) data under the category.
  • The product data is then traversed and displayed in a nested.ulin the list.

3. Display sub-categories in the submenu.

If your website has a deep category hierarchy and you want to directly display the second-level categories under the first-level categories, even the third-level categories, you can achieve this in the following way:

`twig

Related articles

How to display documents under the current category using the `categoryList` tag without including documents from subcategories?

When building a website with Anq CMS, we often encounter such a need: on a category page, we want to display only the documents that belong to the current category, and not mix in the document content of its subcategories.This is particularly important in scenarios where the content structure is clear and avoids redundancy.Today, let's discuss in detail how to accurately achieve this goal through the template tags of Anqi CMS. ### Understanding AnQiCMS Classification and Document Relationship AnQiCMS has always fully considered the flexibility of content hierarchy from the beginning of its design.A category can have subcategories

2025-11-08

How to build a multi-level category navigation in Anqi CMS template and judge whether each category has subcategories?

In today's increasingly rich website content, a clear and efficient navigation system is crucial for improving user experience and search engine optimization (SEO).Especially for websites with a large amount of content or multiple product categories, multi-level category navigation can help users quickly find the information they need while also showing the structural hierarchy of the website to search engines.Anqi CMS with its flexible and powerful template engine makes it simple and intuitive to build such a navigation.This article will deeply explore how to cleverly construct a multi-level classification navigation in Anqi CMS template, and intelligently judge whether each category contains subcategories

2025-11-08

How to retrieve and display custom document parameters for a specific named using the `archiveParams` tag?

In AnQi CMS, content management is not limited to preset fields such as titles, content, etc., but can also be enriched and expanded by custom parameters.When you set unique custom fields for articles, products, or other content models, how to accurately obtain and display these specific parameter names in the front-end template is a common requirement in template creation.This article will deeply explore how to flexibly obtain and display the custom parameters of the `archiveParams` tag.### Custom parameter setting

2025-11-08

How to retrieve all custom parameters of a document in an Anqi CMS template and display them in a fixed order?

In website content management, we often need to define unique attributes for different types of information to meet the diverse display needs of the website.The AnQi CMS provides powerful custom parameter functionality, allowing you to flexibly add additional information to documents (articles, products, etc.)When you want to obtain these custom parameters in the website front-end template and display them in the fixed order set in the background, the built-in template tags of Anqi CMS can well help you achieve this.

2025-11-08

How to determine if the current navigation item is the current page in the `navList` loop?

In website operation, a clear navigation structure is crucial for improving user experience.A good navigation not only helps visitors quickly find the information they need, but also guides users through visual cues (such as highlighting the current page) so that they always know where they are.In AnQi CMS, implementing this feature is quite intuitive and efficient, which is due to its powerful template tag system.

2025-11-08

How to dynamically generate a custom field comment form with `guestbook` tag and adapt to different input types?

Managing website comments in Anqi CMS is a common requirement in daily operations, especially when we need to collect specific information, a flexible and customizable comment form becomes particularly important.Aqie CMS fully considers this requirement and provides a powerful `guestbook` tag, allowing us to easily dynamically generate a feedback form with custom fields and adapt to various input types.The power of the `guestbook` form label The core lies in the `guestbook` tag

2025-11-08

How to display a document's comment list in Anqi CMS template, including reply and pagination features?

In Anqi CMS, add a comment list to the document content and enable it to have reply and pagination functions, which can greatly enhance the user interaction of the website.Users can express their opinions and discuss specific documents, which not only enriches the content ecosystem but also brings more vitality to the website. ### One: Displaying Comment Lists: The Foundation of Interaction To display comments on a document page, it mainly relies on the `commentList` tag built into Anqin CMS.This tag helps us easily get the comment data associated with the current document. First

2025-11-08

How to judge whether the comment is under review and display it conditionally using the `commentList` tag?

In website operation, comments are an important element in enhancing user interaction and community vitality.However, it is an indispensable link to review the comments submitted by users to ensure the health and quality of the website content.How can we flexibly display these comments in AnQi CMS, especially when they are still in the review stage, which has become a concern for many operators.It's good that AnQi CMS template tag system provides us with strong control, especially the `commentList` tag, which can help us easily implement conditional display of comments.

2025-11-08