How to use the `navList` tag to create the main navigation menu of a website and support two-level dropdown display?

Calendar 81

The main navigation menu of the website is an important entry for users to understand the content and structure of the website. Anqi CMS provides powerful and flexiblenavListTags, help us easily create and manage multi-level dropdown navigation menus, thereby improving the user experience and clarity of the information architecture of the website.

The first step: configure the navigation menu in the Anqi CMS backend

Before starting to write the template code, we need to set up the structure of the navigation menu in the Anqi CMS backend management system.

  1. Enter the navigation settings interfaceLog in to the AnQi CMS backend, find 'Backend Settings' in the left menu, click 'Navigation Settings' to enter the management page.
  2. Create or select a navigation category: The system will have a default "default navigation" category, you can use it directly, or click the "navigation category management" button to create new navigation categories as needed, such as "top main navigation", "footer navigation", and so on.Different navigation categories can be applied to different areas of the website.
  3. Add a first-level navigation link:
    • Click the 'Add Navigation Link' button.
    • In the pop-up form, first select 'Parent Navigation' as 'Top-Level Navigation', which means you are creating a main menu item.
    • Enter the "Display Name", this is the text displayed on the website front-end for the menu item.
    • Select the "Link Type", AnQi CMS supports various types of links:
      • Built-in linkFor example, home page, article model home page, product model home page, etc., these are the system predefined common links.
      • Category page linkYou can choose existing article categories, product categories, or individual pages as navigation links.
      • External linkAllow you to customize any URL, whether it is an internal page or an external website.
    • As needed, you can also fill in the 'subheading name' or 'navigation description' to display more rich information on the front end.
    • Set the "Display Order", the smaller the number, the earlier it appears, used to control the arrangement order of menu items.
  4. Add a secondary dropdown navigation link:
    • Click the "Add Navigation Link" button again.
    • A key stepIn the "Parent Navigation", select the primary navigation menu item you just created.
    • Enter information such as "Display Name", "Link Type", etc., similar to the primary navigation item.
    • In this way, you can add multiple secondary sub-menu items for each first-level navigation item. Anqi CMS'snavListThe tag supports the display of two-level dropdown menus by default.

After completing the backend configuration, remember to click the 'Update Cache' button to ensure your changes take effect immediately.

Step two: Apply in the frontend template.navListTag

Once the backend navigation menu structure is set up, the next step is how to call and display these navigation in the website's frontend template.The AnqiCMS template follows the Django template engine syntax, which makes the content structure clear and easy to understand.

通常,你的网站模板文件存放在/templatethe directory and use.htmlas a file extension.

Below is an example of usingnavListTag creates an example code structure that supports two-level dropdown menus:

<nav class="main-navigation">
    <ul class="nav-list-level1">
        {% navList navs with typeId=1 %} {# 这里的typeId=1假设是你的主导航类别ID #}
            {%- for item in navs %} {# 遍历所有一级导航项 #}
            <li class="nav-item-level1 {% if item.IsCurrent %}active{% endif %}">
                <a href="{{ item.Link }}" class="nav-link-level1">{{item.Title}}</a>

                {%- if item.NavList %} {# 判断当前一级导航项是否有二级子导航 #}
                <ul class="nav-list-level2">
                    {%- for inner in item.NavList %} {# 遍历所有二级导航项 #}
                    <li class="nav-item-level2 {% if inner.IsCurrent %}active{% endif %}">
                        <a href="{{ inner.Link }}" class="nav-link-level2">{{inner.Title}}</a>
                        {# 如果需要更多层级的下拉菜单,可以在这里继续嵌套,
                           但需要自定义逻辑或CSS支持,navList标签默认支持两级。
                           这里可以结合其他标签丰富内容,如显示该分类下的文章列表或子分类。 #}
                        {% if inner.PageId > 0 %} {# 假设该二级导航是某个分类或页面链接 #}
                            {% categoryList categories with parentId=inner.PageId %} {# 获取该分类的子分类 #}
                            {% if categories %}
                            <ul class="nav-list-level3">
                                {% for subCategory in categories %}
                                <li><a href="{{ subCategory.Link }}">{{ subCategory.Title }}</a></li>
                                {% endfor %}
                            </ul>
                            {% endif %}
                            {% endcategoryList %}
                        {% endif %}
                    </li>
                    {% endfor %}
                </ul>
                {% endif %}
            </li>
            {% endfor %}
        {% endnavList %}
    </ul>
</nav>

Code explanation:

  • {% navList navs with typeId=1 %}: This is the core tag for calling the navigation list.navsIt is a variable defined to carry all the navigation data of the background configuration.typeId=1Specified the navigation category ID to be called, if you have created multiple navigation categories, please replace it with the corresponding ID.
  • {%- for item in navs %}: This isforLoop, used for iterationnavsEach first-level navigation item in the variable.itemThe variable represents the data of the current navigated first-level navigation item.{%-Its use is to avoid extra blank lines, making the generated HTML cleaner.
  • {{ item.Link }}and{{ item.Title }}: The link address and display name of the current navigation item were output.itemalso includesSubTitle(Subheading),Description(Description) fields, etc., you can call according to your design needs.
  • {% if item.IsCurrent %}:IsCurrentIs a boolean value, if the current navigation item is the page being accessed by the user, it will betrue. This is usually used to add to the current active menu itemactiveA class to highlight through CSS.
  • {%- if item.NavList %}This is the key to determine whether the current first-level navigation item contains sub-navigation.item.NavListIt is an array. If there is data in it, it means there is a second-level navigation, and we will enter the next level of loop.
  • {%- for inner in item.NavList %}: This is nested.forThe loop is used to traverse all the second-level navigation items under the current first-level navigation item.innerThe variable represents the data of the current second-level navigation item being traversed. Its structure isitemsimilar, and also includesLink/Title/IsCurrentfields.
  • {% if inner.PageId > 0 %}This is an advanced usage, demonstrating how to display more rich content in a secondary menu.inner.PageIdIt will return the ID of the navigation item if linked to a category or a single page. By judgingPageIdDoes it exist, we can nest furthercategoryListorarchiveListLabel, to dynamically load the subcategories or article lists under the category, achieving a more powerful dropdown menu function, rather than just simple links.

Advanced techniques and precautions

  1. CSS styles are crucialThe above code only provides the HTML structure. To achieve a beautiful two-level dropdown effect, you need to write the corresponding CSS styles to control the menu layout, display/hide, and animation effects.
  2. Dynamic content fillingWithin the secondary navigation items, if the link type is "category page link", you can utilizeinner.PageIdCombine{% archiveList %}(Document list tag) or{% categoryList %}(Category list label), the associated article list or next-level category will be directly displayed in the drop-down menu

Related articles

How to display all related document lists under a specific `tagDataList` tag and support pagination?

In AnQi CMS' daily content management, we often need to organize and display website content more flexibly.In addition to the traditional classification system, tags (Tag) provide us with another powerful way to associate content.It can aggregate documents from different categories but with similar themes, providing users with a more focused browsing experience.Today, let's delve into a very practical template tag in Anqi CMS, `tagDataList`, and see how it helps us display all related document lists under a specific tag and easily implement pagination.

2025-11-08

How to effectively manage and display single-page content on a website using `pageList` and `pageDetail` tags?

In website operation, single-page content plays an indispensable role, it usually carries important information such as "About Us", "Contact Us", "Terms of Service", "Privacy Policy", etc., which needs to be clearly displayed and also requires convenient management.AnQiCMS provides the powerful template tags `pageList` and `pageDetail`, which help us efficiently manage and display these single-page content.

2025-11-08

How to retrieve and display the name, description, Banner image, and thumbnail of a specific category using the `categoryDetail` tag?

In AnQiCMS template creation, we often need to retrieve and display detailed information about specific categories, such as the name, description, and even preset Banner images and thumbnails.`categoryDetail` tag is born for this, it helps us easily extract this data from the database and flexibly display it on the website front end.

2025-11-08

How to use the `categoryList` tag to build a multi-level category navigation or display category lists in the content area?

In Anqi CMS, properly organizing website content categories is an important aspect for improving user experience and website maintainability.Whether it is to build a clear multi-level navigation menu, help users quickly locate information, or skillfully display different categories of content in the page content area, the `categoryList` tag is the core tool for realizing these functions. ### Core Tag: `categoryList` Description The `categoryList` tag is used specifically in the Anqi CMS template engine to retrieve the website category list.

2025-11-08

How to generate `breadcrumb` tags?

On a day when the content of a website is becoming richer, users often need to navigate between different pages on the website.To help visitors understand their current location and provide a convenient return path, breadcrumb navigation (Breadcrumb Navigation) has emerged.It is like a path indicator on a map, making it clear for the user to know 'Where I came from and where I am going to.'

2025-11-08

How to customize the content model of Anqi CMS to achieve diversified content display?

When building website content, we often find that various types of information have unique display requirements.A news article may require a title, author, publication date, and main content; a product introduction may cover name, model, price, inventory, and detailed parameters; and a case study may focus more on project name, client, solution, and result images.Traditional CMS systems often provide fixed and unchangeable content publication templates, which make it difficult to deal with diverse business scenarios and lack flexibility.

2025-11-08

How to set a unique display layout for different types of articles (such as news, products)?

In website operation, content is not just information itself, but also the way it is presented is equally important.Setting unique display layouts for different types of content (such as news, product introductions) can greatly enhance user experience, strengthen brand image, and even have a positive impact on search engine optimization (SEO).AnQiCMS (AnQiCMS) provides very flexible and powerful features in this aspect, allowing you to easily create exclusive visual styles for various types of content.

2025-11-08

How does the multilingual support of Anqi CMS affect the display and switching of front-end content?

AnQiCMS provides powerful multilingual support capabilities, which is crucial for websites that want to expand into global markets and provide localized content experiences for users of different languages.It not only affects the way the front-end content of the website is presented, but also provides users with a smooth language switching experience.Understanding how it works can help us better plan and operate multilingual websites.

2025-11-08