How to implement flexible navigation menu and submenu display in AnQi CMS template?

Calendar 👁️ 77

In website operation, a clear and intuitive navigation menu is the core of user experience.It not only helps visitors find the information they need quickly, but also directly reflects the structure and content organization logic of the website.AnQiCMS (AnQiCMS) boasts its efficient architecture based on the Go language and the Django-style template engine, providing great flexibility for content creators and developers to achieve various complex navigation menu and submenu display needs.

To implement flexible navigation menus and submenus in the Anqi CMS template, we usually need to combine the background navigation configuration function with the use of front-end template tags.This process is both intuitive and powerful, and can meet various scenarios from simple flat menus to multi-level, dynamic content mixed menus.

Background configuration of flexible navigation menu

Firstly, a flexible navigation menu cannot be separated from careful backend planning.The Anqi CMS provides a "navigation settings" feature, where you can build your website menu system like building blocks.

  1. Navigation category management:You can create different navigation categories, such as 'Top Main Navigation', 'Footer Navigation', 'Sidebar Navigation', etc.This makes it possible to display different menus at different locations on the website, each set of navigation can be managed independently, without interference.
  2. Navigation link settings:You can add specific navigation links under each navigation category. Anqi CMS supports three main types of links:
    • Built-in links:Contains "home link", "article model home page", "product model home page", and other system preset common links for quick addition.
    • Category page links:This is the key to building a dynamic menu. You can choose existing article categories, product categories, or single pages as navigation items.This means that when you add or modify a category or page, the menu will automatically update without manual code modification.
    • External links:Allow you to link to any custom URL within the site, even external websites. This provides convenience for integrating external resources or specific marketing pages.
  3. Hierarchy and sorting:The navigation links of AnQi CMS support up to two levels of nesting, that is, a main navigation item can have one sub-navigation level.You can define the hierarchy of links by setting 'parent navigation' and control the display order of each navigation item accurately by 'display sequence', the smaller the number, the closer to the front.

These backend settings are the foundation for the front-end template to call navigation data, which determines the breadth and depth of your menu content.

The implementation of navigation menus in the template.

The AnQi CMS template engine syntax is similar to Django, variables are enclosed in double curly braces{{ 变量 }}It indicates, logical control such as conditional judgment, loop is represented by single curly braces and percentage signs{% 标签 %}. The navigation menu implementation in the template mainly depends onnavList/categoryListandpageListetc. core tags.

1. UsenavListtags to implement the navigation menu of the background configuration

navListThe tag is the most direct way to call the background "Navigation Settings". It can obtain the main navigation, sub-navigation, and various attributes that you have carefully configured in the background.

Assuming you have already configured a navigation category named "Top Main Navigation" in the background, and it contains first-level and second-level menu items. In the template, you can call it like this:

<nav class="main-navigation">
    <ul>
        {% navList navs with typeId=1 %} {# 假设“顶部主导航”的typeId为1 #}
            {%- for item in navs %}
                <li class="{% if item.IsCurrent %}active{% endif %}">
                    <a href="{{ item.Link }}">{{ item.Title }}</a>
                    {%- if item.NavList %} {# 判断是否有子菜单 #}
                        <ul class="sub-menu">
                            {%- for subItem in item.NavList %}
                                <li class="{% if subItem.IsCurrent %}active{% endif %}">
                                    <a href="{{ subItem.Link }}">{{ subItem.Title }}</a>
                                </li>
                            {% endfor %}
                        </ul>
                    {% endif %}
                </li>
            {% endfor %}
        {% endnavList %}
    </ul>
</nav>

Code analysis:

  • {% navList navs with typeId=1 %}This label will be retrieved from the backgroundtypeIdAll navigation links under the navigation category 1 will be assigned tonavsVariable.
  • {% for item in navs %}Loop through the first-level navigation items.
  • {{ item.Link }}and{{ item.Title }}:Each outputs the link and title of the navigation item.
  • {% if item.IsCurrent %}active{% endif %}:This is a very practical feature.IsCurrentIt will automatically judge whether the current page is the page pointed to by the navigation item, and if it is, it will return.trueYou can use this attribute to add to the currently active menu item.activeTo highlight it through CSS.
  • {% if item.NavList %}To determine if the current top-level navigation item has a submenu.NavListIt is also a list that contains all the sub-navigation items of the navigation item.
  • {% for subItem in item.NavList %}: If there is a submenu, it will traverse the submenu items nestedly, with a logic similar to the first-level menu.

You can flexibly adjust the navigation structure and content in the background in this way, the front-end template does not need to be changed, and it can automatically adapt.

2. UsecategoryListTag builds a dynamic classification menu

For content-driven websites, especially blog or product showcase websites, it is often necessary to use categories as the main navigation structure.categoryListTags can dynamically obtain the category list under the specified content model and is very suitable for building category navigation.

For example, to display article categories under the main navigation, and to display their subcategories in the dropdown menu of the category, and even to list several of the latest articles directly under the subcategories:

`twig

Related articles

How to set the SEO display rules for article, product, page titles and other content in AnQi CMS?

In website operation, the content title is like the facade of the website, it is not only the first impression seen by visitors, but also an important basis for search engines to understand the content of the page and judge its relevance.A meticulously optimized content title that can significantly improve the click-through rate (CTR) and ranking of the website.AnQiCMS (AnQiCMS) as a highly SEO-friendly content management system, has provided us with very flexible and powerful tools to finely set the SEO display rules for articles, products, pages, and other content. Next

2025-11-09

How does the custom content model of AnQi CMS affect the display of front-end data fields?

In website content management, flexibility is the key to improving efficiency and user experience.AnQiCMS (AnQiCMS) performs well in this aspect, with its custom content model feature being particularly powerful.It not only makes content management more organized, but also directly affects the way website front-end data is displayed, making it easy for the website to adapt to various content structures and business needs. ### Understanding Custom Content Model One of the core strengths of Anqi CMS lies in its "flexible content model".

2025-11-09

How to use the recommended attributes of Anqi CMS (such as headlines, sliders) to control the display of homepage content?

When building and maintaining a website, the homepage is undoubtedly the first stop for users to visit, and its way of displaying content directly affects user experience and information communication efficiency.A meticulously planned and flexible homepage layout that can effectively attract visitors, guide traffic, and highlight the core value of the website.AnQiCMS provides a series of powerful content recommendation features, allowing you to easily control the dynamic display of homepage content and meet various operational needs.Flexible use of AnQi CMS recommended attributes AnQi CMS in content management

2025-11-09

How to extract and display the thumbnail and cover image of the document content in Anqi CMS?

In AnQi CMS, managing and displaying document content thumbnails and cover images is an important aspect for enhancing the visual effects of a website, optimizing user experience, and improving search engine friendliness.AnQiCMS provides a flexible mechanism to handle these image resources, whether they are automatically extracted from the content or manually uploaded and specified, they can be managed in detail in the background and called through simple template tags on the front end.

2025-11-09

How to display dynamic breadcrumb navigation and customize its display in Anqi CMS?

When we browse websites, we often see a line at the top or bottom of the page indicating the current navigation path, which is what we usually call 'Breadcrumbs'.It can not only clearly tell the user the position of the current page in the website structure, but also effectively improve the user experience and search engine optimization (SEO) effect of the website.AnQiCMS (AnQiCMS) is a powerful content management system that comes with a convenient way to generate and customize this dynamic breadcrumb navigation.

2025-11-09

How to display filter conditions on the document list page of Anqi CMS and dynamically update results based on parameters?

In AnQi CMS, adding filtering conditions to the document list page and dynamically updating the results based on user selection is an important aspect of improving the discoverability and user experience of website content.This not only helps visitors quickly locate the content they are interested in, but also has a positive impact on SEO through a clear URL structure.Let's understand step by step how to implement this feature in Anqi CMS. ### Core Function: Content Model and Custom Fields are Basic Everything in Anqi CMS is built based on the "Content Model". The filtering function on the document list page

2025-11-09

How to display the links and titles of the previous and next documents in AnQi CMS?

How to easily display the link and title of the previous and next document in AnQi CMS?When browsing website articles, we often hope to smoothly jump from the current content to the previous or next related article. This seamless reading experience not only enhances user satisfaction but also has a positive effect on the overall structure of the website's content and SEO optimization.AnQi CMS is well-versed in this, providing content operators with an extremely convenient way to deploy such navigation functions with its flexible and powerful template engine.

2025-11-09

How to display related recommended documents on the document detail page of AnQi CMS and customize the recommendation logic?

After visitors have read an interesting document or viewed a product introduction on your website, if they can get more relevant recommendations in a timely manner, it will undoubtedly greatly enhance their visit experience, extend their stay time, and encourage them to explore more corners of the website.AnQi CMS knows this and provides flexible and diverse features to help you cleverly display related recommendations on the document detail page, and can customize the recommendation logic according to your operational strategy. We will delve into how to implement this feature in Anqi CMS in detail.###

2025-11-09