How to create a multi-level website navigation menu in AnQiCMS template?

Calendar 👁️ 67

As an experienced AnQiCMS website operator, I know that a clear and efficient multi-level website navigation menu is crucial for user experience and website SEO.AnQiCMS provides a comprehensive mechanism for template design and backend management, making it easy for us to build such a menu.Next, I will elaborate on how to create a multi-level website navigation menu in the AnQiCMS template.

The website navigation is a map for users to browse the website. A well-designed multi-level navigation can effectively guide users to find the information they need and improve the usability of the website.AnQiCMS system is built with powerful navigation management functions, and is supported by flexible template tags, making it simple and direct to create and maintain complex navigation structures.

Set multi-level navigation in the background management

In the AnQiCMS backend, the navigation settings are the foundation for building multi-level menus. First, we need to enter the 'Backend Settings' under the 'Navigation Settings' page.

The system will provide a default "default navigation" category, but you can also create multiple navigation categories as needed, such as "main navigation", "footer navigation", or "sidebar navigation", which is realized through the "navigation category management" feature.Each navigation category can independently manage the navigation links under it, providing great flexibility for navigation in different areas of the website.

In the specific 'navigation link settings', we can define the properties of each navigation item.To create a multi-level navigation, the key is to choose the "parent navigation".When you add a new navigation link:

  • Create a first-level navigation:Set the 'Parent Navigation' to 'Top Level Navigation'.
  • Create secondary navigation:Select an existing top-level navigation as its 'Parent Navigation'.

AnQiCMS currently supports up to two-level navigation links, that is, a first-level navigation can contain a second-level dropdown menu.For each navigation link, you can set its "display name", "subtitle name", and "navigation description", which information can be called in the front-end template.The link type also provides a variety of choices, including "internal links" pointing to content within the website, "category page links", or "external links" pointing to resources outside the website.In addition, the 'Display Order' field allows you to finely control the arrangement of navigation items.

Implement multi-level navigation rendering in the template.

Once the navigation structure is configured in the background, the next step is to render it in the front-end template. AnQiCMS providesnavListtags, which are the core tools for implementing the navigation menu.

We usually place the code snippet of the navigation menu in the template.partial/in the directory, for examplepartial/header.htmlThen proceed{% include "partial/header.html" %}This modular design helps in code reuse and maintenance.

Inpartial/header.htmlIn the (or other navigation template file) usenavListtags to obtain the navigation data set in the background. If you have created multiple navigation categories in the background, you can navigate throughtypeIdSpecify the navigation category to be called, for example{% navList navs with typeId=1 %}Indicates the navigation category called with ID 1 (usually the default navigation).

navListThe tag will treat navigation data as an array object, such asnavsReturn. To render a multi-level menu, we need to use nestedforloop to iterate through this array:

<ul>
    {% navList navs with typeId=1 %} {# 假设typeId=1是主导航 #}
    {%- for item in navs %}
        {# 判断当前导航项是否为活跃状态,添加active类名 #}
        <li class="{% if item.IsCurrent %}active{% endif %}">
            <a href="{{ item.Link }}">{{item.Title}}</a>
            {%- if item.NavList %} {# 检查是否存在下级导航 #}
            <ul class="submenu"> {# 二级导航菜单容器 #}
                {%- 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 %}
    {% endnavList %}
</ul>

In this example:

  • The outermost{% for item in navs %}Loop through all first-level navigation items
  • item.IsCurrentVariables can be used to determine whether the current navigation item is the page the user is visiting, making it convenient to addactiveCSS classes to highlight.
  • item.Linkanditem.TitleUsed to output the link address and display name of the navigation item.
  • {%- if item.NavList %}It is a key conditional judgment that checks whether the current first-level navigation item contains sub-navigation.NavListIsitemThe object contains an array of second-level navigation items.
  • If there is a sub-navigation, it is nested.{%- for inner in item.NavList %}The loop will traverse all secondary navigation items and output their links and titles as well.

In this way, we can dynamically render the multi-level navigation menu configured on the back-end in the front-end template.

Key considerations

When creating multi-level navigation menus, in addition to the above steps, there are some practices and precautions:

  • Level limit:Remember that AnQiCMS currently supports up to two levels of navigation. If your website needs a deeper navigation hierarchy, you may need to consider combining other content list tags such ascategoryListorarchiveList) to simulate a deeper level of content display, rather than a pure navigation menu structure.
  • Modular:Make full use ofincludeThe tag divides navigation code into reusable small modules. This not only makes the template code neater, but also facilitates team collaboration and maintenance.
  • Responsive design:Considering the experience of mobile device users, it is necessary to cooperate with CSS and JavaScript for responsive multi-level navigation, ensuring that it can be displayed and interacted with well on different screen sizes.
  • Cache Update:After modifying the navigation settings in the background, if the front-end does not update in time, please remember to clear the system cache to ensure that the latest configuration takes effect.

By using AnQiCMS's flexible backend configuration and powerful template tags, we can effectively build a multi-level website navigation menu that is both beautiful and practical, thereby enhancing user experience and the overall efficiency of the website.


Frequently Asked Questions

Q1: How many levels does AnQiCMS navigation menu support?

The AnQiCMS backend navigation settings support up to two-level navigation menus, that is, a first-level navigation item can contain a hierarchical submenu.This means you can create a main menu item and its direct dropdown submenu.

Q2: How can I highlight the navigation menu item corresponding to the current page?

In the template,navListThe label returns each navigation item object (itemandinner) contains aIsCurrentfield. This field is returned when the current page matches the navigation item linktrue. You can use conditional judgments in the template, such asclass="{% if item.IsCurrent %}active{% endif %}"Add a specific CSS class to the currently active navigation item to achieve a highlight display effect.

Q3: Can I set different navigation menus for different areas of the website (such as the top, sidebar, footer)?

Absolutely. The navigation settings on AnQiCMS backend provide the navigation category management feature, allowing you to create multiple independent navigation categories.For example, you can create categories such as 'main navigation', 'footer navigation', and so on, and then add and manage navigation links separately for each category.In the template, just by passing throughnavListlabel'stypeIdThe parameter specifies the corresponding navigation category ID, which can call and render different navigation menus.

Related articles

How to remove the blank lines that may be generated by logic tags in the AnQiCMS template?

As an experienced CMS website operation personnel of an enterprise, I know that every detail of content presentation is crucial, including seemingly trivial blank lines.In a meticulously constructed template, unnecessary blank lines not only affect the neatness of the HTML code, but may also cause unexpected visual problems in certain layouts.Today, let's delve into how to efficiently remove blank lines that may be generated by logical tags in the AnQiCMS template.

2025-11-06

How to dynamically display the home page banner list in AnQiCMS template?

As a senior who has been deeply engaged in AnQiCMS content operation for a long time, I know the importance of the homepage banner for the visual appeal and marketing promotion of the website.A well-designed and dynamic Banner list that can quickly attract visitors' attention and effectively convey the latest product, service, or event information.Today, I will demonstrate in detail how to dynamically display the Banner list on the website homepage based on the powerful template function of AnQiCMS.

2025-11-06

How to call the switch link of multi-language sites in AnQiCMS template?

As a senior CMS website operator for an enterprise security company, I know that the multilingual site switching link is crucial for expanding the international market and improving user experience.AnQi CMS provides us with a convenient and efficient solution with its powerful multi-site and multi-language support function.Below, I will elaborate on how to call the multilingual site switch link in the AnQiCMS template.Flexible construction of multi-language site switching function Anqi CMS has fully considered the needs of enterprises and operators in global layout from the beginning of its design, originally supporting multi-site management and multi-language content switching

2025-11-06

How to get the thumbnail version of the image in AnQiCMS template?

As an experienced CMS website operation personnel, I am well aware of the importance of high-quality content and excellent user experience.In the visual presentation of a website, images play a core role, and the optimization of images, especially the application of thumbnails, is invaluable for improving page loading speed, enhancing user experience, and improving search engine rankings.The AnQi CMS provides strong and flexible support in this aspect, allowing operation personnel to easily obtain and display thumbnail versions of images in templates.In today's content-driven internet environment, it has become commonplace for web pages to contain a large number of images. However

2025-11-06

How to customize the directory structure and file naming rules of AnQiCMS templates?

Hello, as a senior operator of AnQiCMS, I am very happy to be able to elaborate in detail on how to customize the directory structure and file naming rules of AnQiCMS templates.Understanding these basics is the key to efficient management and optimization of website content. ### AnQiCMS Template Custom Overview AnQiCMS is an enterprise-level content management system based on the Go language, which provides high flexibility in template customization.It uses a syntax similar to the Django template engine, making it easy for front-end developers to get started.

2025-11-06

How to specify an independent template file for a specific category, article, or single page in AnQiCMS?

As a deep user of Anqi CMS website operators, I know that the flexibility of content display is crucial for attracting and retaining users.Strong support is provided by Anqicms in this aspect, allowing us to specify exclusive template files for specific categories, articles, even independent pages, thereby achieving highly customized content presentation.This not only helps to improve the user experience, but also provides great convenience for our refined operation and SEO optimization.

2025-11-06

How to get and display the global configuration information (such as name, Logo) of AnQiCMS template?

As an experienced website operator proficient in AnQiCMS, I know that efficiently and accurately obtaining and displaying the global configuration information of the website is crucial for website maintenance, brand consistency, and user experience.AnQiCMS provides intuitive and powerful template tags, making it easy to achieve this goal.In AnQiCMS template, obtain and display the global configuration information of the website, such as the website name, Logo, filing number, etc., mainly depending on the built-in `system` tag

2025-11-06

How to call and display the contact information (such as phone, email, social media) configured in the AnQiCMS template?

As a senior content worker who deeply understands the operation of Anqi CMS, I know the importance of displaying the contact information of the enterprise externally.This not only concerns whether customers can conveniently contact you, but also is the basis for building trust and promoting business cooperation.AnQi CMS provides a直观 and flexible solution for website operators, allowing you to easily manage and display various contact information without writing complex code.

2025-11-06