How to use the `forloop.Counter` variable in the AnQiCMS navigation list tag in multi-level navigation?

Calendar 👁️ 84

As an experienced website operations expert, I am well aware of the importance of an efficient and flexible CMS system for content management and user experience.AnQiCMS (AnQiCMS) provides powerful support for content creators and operators with its high-performance architecture based on the Go language and Django-style template engine.In AnQiCMS template development, flexibly using various tags is the key to improving website interactivity and maintainability.forloop.CounterHow to play a unique role in the multi-level navigation list of AnQiCMS.

In the AnQiCMS navigation list tag.forloop.CounterVariable: Fine-grained control of multi-level navigation.

In AnQiCMS, the construction of website navigation usually takes advantage of strongnavListThe label allows us to dynamically retrieve and render out hierarchical navigation menus, which is crucial for maintaining a clear and easy-to-navigate website.navListLabel collaborationforThe loop can easily traverse the top-level navigation items and their nested sub-navigation lists. During this traversal,forloop.CounterVariables are like a Swiss Army knife in our hands, helping us to have more fine-grained control and customization over navigation items.

Understanding the navigation structure of AnQiCMS'sforloop.Counter

First, wenavListLabel to get navigation data, usually assigned to a variable, for examplenavs. ThisnavsThe variable is an array object containing all top-level navigation items. Each navigation item may also contain a name calledNavListThe sub-navigation list, thus forming a multi-level navigation structure.

When we use in the template{% for item in navs %}When such a loop statement is used to traverse the navigation items, the AnQiCMS template engine will automatically provide a special variable namedforloopfor the current loop.forloopVariables embedded multiple useful properties, among whichforloop.Counteris the current loop iteration count, starting from 1. At the same time, there is also aforloop.RevcounterIt indicates the remaining number of iterations from the current item. These counters are especially convenient when dealing with the start, end, or specific elements of a list.

It is worth emphasizing that every timeforEach loop will have its own context. This means that when you are using a nestedforlooploop (for example, iterating through secondary navigation items)forin a loop.forloop.CounterIt will start counting from 1 again, instead of continuing the count from the outer loop. This feature is the basis for our implementation of multi-level navigation fine-grained control.

Usefully in multi-level navigationforloop.Counter

In the operation of actual websites, we often encounter such needs: to add numbering to the navigation menu, to apply special styles to the first or last menu item, or to generate a unique ID for menu items of a specific level.forloop.CounterVariables can perfectly handle these scenarios.

Imagine that we want to add clear numbering to the main navigation menu of the website and its sub-menus, such as "1. Home", "2. About Us", "2.1. Company Profile", "2.2. Development History", and so on.At this time, we can get the top-level navigation items in the outer loop.forloop.CounterAnd get the second-level navigation items in the inner loop.forloop.CounterAnd then combine them cleverly.

The following is a specific code snippet demonstrating how to implement this feature in the AnQiCMS template:

{% navList navs %}
<ul>
    {%- for item in navs %}
        {# 捕获外层循环的当前计数,以便在内层循环中使用 #}
        {% set parent_counter = forloop.Counter %}
        <li class="nav-item-{{ parent_counter }} {% if item.IsCurrent %}active{% endif %}">
            <a href="{{ item.Link }}">{{ parent_counter }}. {{ item.Title }}</a>
            {%- if item.NavList %}
            <dl class="sub-nav">
                {%- for inner in item.NavList %}
                    <dd class="sub-nav-item-{{ parent_counter }}-{{ forloop.Counter }} {% if inner.IsCurrent %}active{% endif %}">
                        <a href="{{ inner.Link }}">{{ parent_counter }}.{{ forloop.Counter }}. {{ inner.Title }}</a>
                    </dd>
                {% endfor %}
            </dl>
            {% endif %}
        </li>
    {% endfor %}
</ul>
{% endnavList %}

In this code, we first use{% set parent_counter = forloop.Counter %}Assign the current iteration count of the outer loop (i.e., the top-level navigation) to a temporary variable namedparent_counter. Thissettag (orwithTags allow us to define a variable within the current scope and this variable can be accessed within the nested loop inside it.

Then, in the display of the top-level navigation items, we used it directly{{ parent_counter }}to display the main number. When entering the secondary navigationforloop, we used it again{{ forloop.Counter }}. Since this is another independentforThe loop, itsforloop.Counterwill start counting from 1 again, perfectly providing us with secondary numbering. By combining{{ parent_counter }}.{{ forloop.Counter }}We can generate clear multi-level numbering, such as '1.1', '1.2', and so on.

In addition to the numbering,forloop.Counterit can also help us achieve more creativity and functions in navigation:

  • Dynamic CSS class name: Generate a unique CSS class for each navigation item, for examplenav-item-1/sub-nav-item-2-1It facilitates front-end developers to customize the styles of specific or specific navigation items without manually modifying the HTML structure.
  • Special treatment for first and last items: By judgingforloop.Counter == 1Identify the first element or utilizeforloop.Revcounter == 1(orforloop.Last

Related articles

How to integrate search boxes, shopping carts, and other functional elements into the AnQiCMS navigation menu?

##驾驭AnQiCMS navigation: Skillfully integrate search, shopping cart and other functional elements to create an efficient user experience As an experienced website operation expert, I know the importance of an efficient and user-friendly navigation menu for the success of the website.In today's era of information overload, users expect to quickly find the content they need, and elements such as search boxes and shopping carts, if cleverly integrated into navigation, can undoubtedly greatly enhance user experience and website conversion rates.AnQiCMS, this is an enterprise-level content management system built based on the Go language, with its high efficiency, customizable, and easy to expand features

2025-11-07

Does the AnQiCMS navigation menu display different navigation structures according to device type (PC/Mobile)?

Good, as an experienced website operation expert, I am very willing to deeply analyze the ability of AnQiCMS in the differentiation display of navigation menus and transform it into an article that is easy to understand and practical. --- ## Safe CMS Navigation Menu: The Secret of PC/Mobile End Differentiation Display In today's multi-screen interconnection era, users visit websites with various types of devices.Whether it is the wide screen on the PC end or the compact interface on the mobile end, users expect to get a smooth and intuitive browsing experience.

2025-11-07

How to display dynamic data in the AnQiCMS navigation menu, such as the number of unread messages?

As an experienced website operations expert, I fully understand the core status of the navigation menu in user experience and website architecture.It is not only the index of the content, but also the key to users quickly obtaining the information they need and improving conversion efficiency.Today, we will delve into how to cleverly integrate dynamic data into the navigation menu of AnQiCMS (AnQi CMS), such as the number of unread messages that everyone cares about, which will undoubtedly greatly enhance user interaction and the vitality of the site.### Insight requirements: The challenge of integrating static navigation with dynamic data Traditional website navigation menus are often static.

2025-11-07

Does AnQiCMS have a built-in caching mechanism when fetching data from the navigation list tags?

Dear operations partners, hello!As an expert with many years of experience in website operations, I am well aware of the importance of website performance for user experience and Search Engine Optimization (SEO).AnQiCMS, with its excellent performance and flexibility, has gained a place in the field of content management.Today, we will delve deeply into a common concern: Does AnQiCMS's navigation list tag have a built-in caching mechanism when fetching data?This is not only about the technical implementation, but also closely related to the efficiency of our daily content operation.

2025-11-07

How does AnQiCMS template truncate and display the description content of a navigation item if it is very long?

As an experienced website operations expert, I am well aware that in today's user experience-oriented era, even the details such as website navigation contain huge optimization space.AnQiCMS as an efficient and flexible content management system, its powerful template function provides us with enough 'magic' to meet various front-end display challenges.Today, let's talk about a common yet often overlooked issue: how should we elegantly truncate the display of navigation item descriptions in the AnQiCMS template.### Optimize Navigation Experience

2025-11-07

How to combine AnQiCMS navigation tags with TDK information in `tag-tdk.md` for page title optimization?

As an experienced website operation expert, I know that every detail is crucial for the visibility and user experience of the website, especially the core elements of search engine optimization (SEO) - page title (Title), keywords (Keywords) and description (Description), which we usually call TDK.AnQiCMS (AnQiCMS) took this into consideration from the beginning of its design, with its powerful TDK management functions and flexible template tag system, which can help operators efficiently build SEO-friendly websites. Today

2025-11-07

How to pass specific variables to the navigation template of the AnQiCMS template using the `with` tag?

How to pass specific variables to the navigation template of the AnQiCMS template through the `with` tag?As an expert well-versed in website operations, I often encounter such a need: in order to maintain the neatness and maintainability of the website code, we usually break down commonly used modules such as headers, footers, sidebars, and navigation into independent template fragments.AnQiCMS (AnQiCMS) with its efficient architecture based on Go language and Django template engine syntax, provides us with great convenience. However

2025-11-07

Does the AnQiCMS navigation menu backend management interface provide batch operation functions?

## AnQiCMS navigation menu: Does it currently support batch operations?Expert interpretation and practical suggestionsFor website operators, efficiently managing navigation menus, especially in scenarios where frequent adjustments or large-scale updates are required, the need for batch operation features becomes particularly prominent.

2025-11-07