In today's increasingly rich website content, a clear and efficient navigation system is crucial for enhancing 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 displaying 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 navigation.This article will deeply explore how to cleverly construct multi-level category navigation in the Anqi CMS template, and intelligently judge whether each category contains subcategories, making your website navigation more dynamic and intelligent.

Understand the template basics of Anqi CMS

The AnQi CMS template system adopts a syntax similar to the Django template engine, which is very friendly to developers and easy to learn.It allows us to dynamically display data through specific tags and variables.When building category navigation, it is mainly usedcategoryListLabel, as wellifandforSuch logic control tags. These tags together constitute the foundation for us to implement complex navigation logic.

The core of building multi-level classification navigation:categoryListTag

categoryListThe tag is designed specifically for obtaining the category list in AnQi CMS, it can filter out different models and categories at different levels according to our needs.

  • Specify the content model (moduleId)Your website may contain articles, products, and various content models. Categories under each model are independent. ThroughmoduleIdThe parameter allows you to specify precisely which category of content model you want to retrieve. For example,moduleId="1"typically represents an article model,moduleId="2"may represent a product model.
  • Specify the parent category (parentId)This is the key to implementing multi-level navigation.
    • WhenparentId="0"Then, you will retrieve all top-level categories under the specified content model.
    • WhenparentIdSet the ID of a specific category, and it will retrieve all direct subcategories under that category.
    • If you want to get the sibling categories of a certain category (i.e., all child categories that belong to the same parent category), you can do so by using the parent category ID if it is known.parentIdtransmittedcategoryList.

Does the category have subcategories:item.HasChildren

In the loopcategoryListWhen the category data obtained by the tag is, each category (usually named in the loop) has a very useful attribute:itemEach category (usually in parentheses) has a very useful attribute:HasChildren. This is a boolean value, if the current category has subcategories,HasChildrenThe value istrue; otherwise,false.Using this property, we can flexibly control the display logic of navigation in the template, for example, only showing the container of the secondary menu under items with subcategories, thus avoiding unnecessary empty menus.

Actual operation: build multi-level category navigation

Now, let's take a specific example to show how to build a two-level or more hierarchical classification navigation in the template.

Assuming we want to build a navigation menu that displays article categories, including primary categories and their subcategories:

`twig {# Get all top-level categories under the article model (assuming moduleId is 1) #} {% categoryList categories with moduleId=“1” parentId=“0” %}