How to implement the dynamic display of multi-level navigation menus in front-end templates?

Calendar 👁️ 86

In web design, a clear and easy-to-use navigation menu is the key to improving user experience and the professionalism of the website.AnQiCMS (AnQiCMS) provides the convenience of dynamically building multi-level navigation menus in front-end templates with its flexible template engine and powerful content management features.Understanding the backend configuration and the calling logic of the front-end template of its navigation system is the foundation for achieving this goal.

Anqi CMS Navigation System Overview

The navigation function design of AnQi CMS is very practical, closely integrating the creation of the navigation menu with content display.On the backend, we can configure the navigation structure through an intuitive interface;On the front end, it utilizes a tag system based on the Django template engine syntax, enabling highly flexible dynamic rendering of these menus.The entire process aims to reduce technical complexity to the lowest, allowing content operators and front-end developers to easily get started.

Backend Management: Building Navigation Menu

Firstly, we need to set up the navigation menu in the Anqi CMS backend management interface. This is usually completed under the 'Backend Settings' and 'Navigation Settings'.

  1. Navigation category management:The AnQi CMS allows the creation of multiple navigation categories, such as "top navigation", "bottom navigation", "sidebar navigation", and so on.This allows us to customize independent navigation structures for different areas of the website, greatly enhancing the flexibility of the website layout.By adding new navigation categories, you can create exclusive menus for specific display locations (such as the footer).
  2. Navigation link settings:Under the selected navigation category, we can add specific navigation links.Each link can be set to display name, subtitle name, and navigation description, which information can be called in the frontend template.The most important is the "link type", Anqi CMS provides three flexible link methods:
    • Built-in links:Include 'Home link', 'Content model home page' (such as article list, product list home page), etc., for quick access to frequently used pages.
    • Category page links:This can be directly associated with a document category or a single page. This means we can make the navigation menu items point directly to a product category page or an independent page like 'About Us'.
    • External links:Great freedom is provided, can link to any custom URL within the site, even to other websites outside the site. It is worth noting that Anqi CMS currently supportsup to two levelsNavigation link. This means we can create main menu items and their direct sub-menu items.During the setup process, the system will also provide an option for 'display order' for each menu item, so that we can finely control the arrangement of the menu.

Front-end template: Dynamic rendering of multi-level navigation

In the front-end template, dynamic display of multi-level navigation mainly depends on the Anqi CMS providednavListTags and basic loop and conditional judgment statements. The template files of Anqi CMS use.htmlsuffix, static resources are usually stored in/public/static/The catalog supports tag syntax similar to Django ({{变量}}Used for variable output,{% 标签 %}and{% end标签 %}Used for logical control)。

  1. InvokenavListTags: navListThe tag is the core of obtaining navigation data. It allows us to navigate based on the "navigation category ID" set on the backend (typeIdTo get the corresponding navigation list. For example, if your top navigation category ID is 1 (usually it is 1 by default), you can call it like this:

    {% navList navs with typeId=1 %}
        {# 在这里循环输出导航菜单 #}
    {% endnavList %}
    

    HerenavsIt is a custom variable name that will contain all the navigation menu items under the category.

  2. Traverse the first-level navigation menu: navsThe variable is an array object, we need to useforLoop through it to display the top-level navigation menu. Eachitem(or a custom loop variable you define) contains various properties of the navigation menu items, such asitem.Title(Display name),item.Link(link address) anditem.IsCurrentCheck if it is the current page link.

    <ul class="main-nav">
        {% for item in navs %}
            <li class="{% if item.IsCurrent %}active{% endif %}">
                <a href="{{ item.Link }}">{{item.Title}}</a>
            </li>
        {% endfor %}
    </ul>
    

    item.IsCurrentThis property is very useful, it allows us to easily add CSS styles to the navigation menu item corresponding to the current visited page, such as adding aactiveclass to provide visual feedback.

  3. Render a second-level navigation menu:Because AnQi CMS supports two-level navigation,itemThe object may contain a namedNavListsub-navigation list. We need to check through a conditional judgment to determineNavListDoes it exist, if it exists, then it will nest oneforloops to render the second-level menu.

    <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 %} {# 判断是否存在子导航 #}
                <ul class="sub-nav">
                    {% for inner in item.NavList %}
                        <li class="{% if inner.IsCurrent %}active{% endif %}">
                            <a href="{{ inner.Link }}">{{inner.Title}}</a>
                        </li>
                    {% endfor %}
                </ul>
                {% endif %}
            </li>
        {% endfor %}
    </ul>
    

    Thus, a basic multi-level navigation menu is displayed dynamically

Extended Application: Dynamic content in the navigation menu

The strength of Anqi CMS lies in its ability to further integrate dynamic content in the navigation menu, such as displaying the latest articles or products under the category in the secondary menu. This requires combiningarchiveListorcategoryListImplement tags.

  1. Display documents under categories in the navigation menu:Assuming your secondary navigation links to a category, you want to display the latest few articles or products under this menu item. When configuring navigation links in the backend, select "Category Page Link" and associate it with a specific category, the navigation item will beinner.PageIdThe attribute will store the ID of the category. We can use this ID to call related documents.

    {# ... 二级导航循环内部 ... #}
    {% if inner.NavList %} {# 判断是否存在子导航 #}
        <ul class="sub-nav">
            {% for inner in item.NavList %}
                <li class="{% if inner.IsCurrent %}active{% endif %}">
                    <a href="{{ inner.Link }}">{{inner.Title}}</a>
                    {# 在这里调用与该分类相关的文档 #}
                    {% archiveList products with type="list" categoryId=inner.PageId limit="8" %}
                    {% if products %}
                    <ul class="nav-content-list">
                        {% for product in products %}
                        <li><a href="{{product.Link}}">{{product.Title}}</a></li>
                        {% endfor %}
                    </ul>
                    {% endif %}
                    {% endarchiveList %}
                </li>
            {% endfor %}
        </ul>
    {% endif %}
    {# ... #}
    

    This code will be below each secondary navigation item ifinner.PageId(i.e., the associated category ID) exists, list the 8 latest documents under the category (products or articles, depending onarchiveListofmoduleIdsettings).

  2. Display subcategories in the navigation menu:A common need is to display the deeper level subcategories of the category belonging to the secondary navigation items below. This can be done by callinginner.PageId(if it is associated with a category) again.categoryListto achieve.

    "twig {# ... secondary navigation loop inside ... #} {% if inner.NavList %} {# Check if there is a sub-navigation #}"}

    <ul class="sub-nav">
        {% for inner in item.NavList %}
            <li class="{% if inner.IsCurrent %}active{% endif %}">
                <a href="{{ inner.Link }}">{{inner.Title}}</a>
                {% if inner.PageId > 0 %} {# 确保PageId有效,且通常是一个分类ID #}
                    {% categoryList subCategories with parentId=inner.PageId %}
                    {% if subCategories %}
                    <ul class="nav-sub-categories">
                        {% for subCat in subCategories %}
    

Related articles

How to implement lazy loading (Lazyload) of images in the article content to optimize page performance?

In website operation, page loading speed is crucial for user experience and search engine optimization (SEO).Especially when the article content includes a large number of images, the loading of image files often becomes the "culprit" that slows down the page speed.Lucky enough, by implementing the Lazyload image technology, we can effectively solve this problem.For friends using AnQiCMS, understanding and applying this technology will significantly improve website performance. Image lazy loading, as the name implies, is to delay the loading of images outside the user's current viewport.When the user scrolls the page

2025-11-07

How does AnQiCMS ensure the safety and compliance of front-end displayed content, such as sensitive word filtering?

When operating a website, the security and compliance of content is an indispensable part, especially in the context of the increasingly regulated online environment today.For users who use AnQiCMS, the system provides a variety of functions to help ensure the security and compliance of the displayed content on the front end, such as the sensitive word filtering that users are generally concerned about.At the beginning of its design, AnQiCMS placed content security at the core position.It is not just a content publishing tool, but also a platform dedicated to providing users with a safe and compliant content management environment

2025-11-07

How to call and display the contact information (phone, email, address) set in the back-end template on the front-end?

It is crucial to clearly and effectively display contact information in website operation to establish user trust and promote communication.Whether it is to consult products, seek service support, or give feedback, a reachable phone, email, or address can greatly enhance the user experience.AnQiCMS (AnQiCMS) fully considers this requirement, providing a flexible and convenient way for website administrators to centrally manage contact information in the background, and easily call and display on the front-end page through simple template tags.### Step 1

2025-11-07

Does AnQiCMS support directly retrieving and displaying system configuration information (such as website name, Logo) in templates?

During the process of building and maintaining a website, we often encounter the need to display basic information about the website at various positions on the page, such as the name of the website, Logo, copyright statement, filing number, and so on.If this information is scattered in all corners of the template, it will be very cumbersome and prone to errors once it needs to be modified.What kind of solution does AnQiCMS provide in this regard?Does it support us to directly obtain and display these system-level configuration information in the template?The answer is affirmative.

2025-11-07

How to correctly display the list of related articles on the content detail page in AnQiCMS?

On AnQiCMS content detail page, displaying related article lists can effectively guide visitors to browse more content, reduce the bounce rate, and enhance the website's SEO performance through internal link structure.AnQiCMS is a system designed specifically for content operations, providing flexible and powerful template tags, allowing us to easily achieve this function.How does AnQiCMS recognize and display related articles?To make the content detail page intelligently display a list related to the current article, AnQiCMS provides a straightforward mechanism

2025-11-07

How to display custom product parameters and specifications on the product detail page?

Add custom parameters and specifications to the product detail page in AnQiCMS, which can make your product information more detailed and professional.Whether it is the technical parameters of electronic products or the size and color options of clothing, you can easily achieve these personalized display requirements through a flexible content model.Next, we will step by step understand how to set, fill in, and finally display these customized parameters and specifications on your product detail page.--- ### Step 1: Define product custom parameters in the background Start adding custom parameters to the product

2025-11-07

How to set the display logic and hierarchy of breadcrumb navigation in AnQiCMS template?

When building a website, breadcrumb navigation not only effectively improves user experience and helps visitors understand their current location, but is also an indispensable part of search engine optimization (SEO), as it clearly shows the hierarchical structure of the website.For websites using AnQiCMS to manage content, flexibly setting the display logic and hierarchy of breadcrumb navigation is an important task in template creation.AnQiCMS provides a powerful and easy-to-use template tag for handling breadcrumb navigation, allowing you to easily implement it in the front-end template without writing complex backend logic

2025-11-07

How is the 'Multi-site Management' feature of AnQiCMS implemented to display different site content independently?

## Deeply analyze the "Multi-site Management" feature of AnQiCMS: How to ensure independent display of content?How to efficiently manage content when operating multiple websites or owning multiple brand sub-sites, while ensuring the independence of content on each site, is a challenge faced by many operators.The "multi-site management" feature provided by AnQiCMS is designed to solve this pain point.It not only allows you to manage all sites in a single background, but also fundamentally ensures that the content of each site is displayed independently, avoiding confusion and errors.So, AnQiCMS

2025-11-07