How to judge whether the current item in the list loop is the first or last in the AnQiCMS template?

Calendar 👁️ 64

Master the list loop in AnQiCMS template: A clever trick to identify the first and last items easily

In website content operation, dynamically displaying data lists is a very common requirement.Whether it is an article list, product display, or image gallery, we hope to make the information more attractive through unique visual effects or logical processing.Sometimes, this means we need to apply special styles to the first or last item in the list, such as adding a "new" tag to the first article or not displaying the right border on the image at the end of the list.

AnQiCMS is an enterprise-level content management system developed based on the Go language, which provides great convenience for content operation teams with its efficient and customizable features.Its template engine inherits the concise syntax of Django templates, making developers and operators able to quickly get started.Today, let's delve deeply into a very practical skill in AnQiCMS template creation: how to accurately judge whether the current item in the list loop is the first or the last one.

Basic of AnQiCMS list loop:forTags and built-in variables

In AnQiCMS template, we usually use{% for item in collection %}such a structure to iterate over the dataset.collectioncan bearchiveList/categoryList/pageListany list data returned by the tags. ThisforThe power of loops lies not only in their ability to iterate over data but also in some special built-in featuresforloopVariables, they provide context information for the current loop, which is the key to implementing the judgment of the first and last items.

Among them, the two most core variables areforloop.Counterandforloop.Revcounter.

1. Determine if the current item is the first: Useforloop.Counter

forloop.CounterThe variable will return the current loop count, which is an integer starting from 1. Therefore, whenforloop.CounterWhen the value is 1, we can determine that the current item being traversed is the first item in the list.

We can cooperate{% if %}With conditional judgment tags to easily achieve this:

{% if forloop.Counter == 1 %}
    {# 这是列表中的第一条数据,可以应用专属样式或逻辑 #}
{% endif %}

For example, if you want to add a CSS class to the first item in the list,first-itemyou can do it like this:

<div class="{% if forloop.Counter == 1 %}first-item{% endif %}">
    <!-- 列表项内容 -->
</div>

2. Determine whether the current item is the last one by usingforloop.Revcounter

withforloop.Counterdifferent,forloop.RevcounterThe variable returns how many more items are left to the end of the list, including the current item. This means that whenforloop.RevcounterWhen the value is 1, the current item being traversed is the last item in the list.

Similarly, we use{% if %}tags to judge:

{% if forloop.Revcounter == 1 %}
    {# 这是列表中的最后一条数据,可以应用专属样式或逻辑 #}
{% endif %}

If you want to add an item to the last item of the list,last-itemThe CSS class, the code will be like this:

<div class="{% if forloop.Revcounter == 1 %}last-item{% endif %}">
    <!-- 列表项内容 -->
</div>

Combine the example, flexibly use the first and last item judgment

After understanding these two core variables, let's look at some practical application scenarios to feel their practical value.

Scenario one: Add unique CSS styles to the first and last items.

Assume we have a list of articles, and we want the title of the first article to be bold, and the background color of the last article to be lightened.

{% archiveList articles with type="list" limit="5" %}
    {% for article in articles %}
        <div class="article-item {% if forloop.Counter == 1 %}first-article{% endif %} {% if forloop.Revcounter == 1 %}last-article{% endif %}">
            <h3 class="{% if forloop.Counter == 1 %}font-bold{% endif %}">
                <a href="{{ article.Link }}">{{ article.Title }}</a>
            </h3>
            <p>{{ article.Description|truncatechars:100 }}</p>
            {% if forloop.Revcounter == 1 %}
                <p class="text-muted">—— 本系列文章已完结 ——</p>
            {% endif %}
        </div>
    {% endfor %}
{% endarchiveList %}

By this means, we can accurately add or modify properties for the first and last items dynamically, making the page visual effects more rich without having to manipulate the DOM with JavaScript.

Scenario two: Insert separators between list items, but avoid them after the last item

This is a very common requirement, such as the Tag tag list, we usually use commas or vertical bars to separate, but there should be no separator after the last tag.

{% tagList tags with itemId=currentArchive.Id limit="5" %}
    {% for tag in tags %}
        <a href="{{ tag.Link }}">{{ tag.Title }}</a>{% if forloop.Revcounter != 1 %}, {% endif %}
    {% endfor %}
{% endtagList %}

The logic is that, as long as the current item is not the last one (i.e.forloop.Revcounteris not 1), a comma and a space is added to the end.

Scenario three: Special content is displayed only on the first item, such as the "new" badge

In the news or blog list, we may want to highlight the first article published most recently in a prominent way.

{% archiveList news with type="list" limit="3" order="CreatedTime desc" %}
    {% for item in news %}
        <div class="news-entry">
            {% if forloop.Counter == 1 %}
                <span class="badge new-badge">最新!</span>
            {% endif %}
            <a href="{{ item.Link }}">{{ item.Title }}</a>
            <span class="publish-date">{{ stampToDate(item.CreatedTime, "2006-01-02") }}</span>
        </div>
    {% endfor %}
{% endarchiveList %}

Only whenforloop.CounterWhen the value is 1, the "New!" badge will be displayed, providing users with a direct guide.

Related articles

How to use the `if` tag to check if `archiveList`, `categoryList`, etc., are empty?

As an experienced website operations expert, I know that paying attention to the user experience in content management and website presentation is crucial.A meticulously designed website that not only provides rich content but also maintains elegance and usability even when content is missing.How can one effectively judge whether the list data is empty and flexibly display content accordingly, which is a basic and key skill in the design of front-end templates.In AnQiCMS (AnQiCMS), we take advantage of its powerful and easy-to-use Django template engine syntax, which can very elegantly achieve this.

2025-11-06

How to check for content inclusion using the `{% if "keyword" in variable %}` condition in AnQiCMS templates?

As an experienced website operations expert, I know that the flexible use of templates is the key to improving website efficiency and user experience in daily content management.AnQiCMS (AnQiCMS) provides us with powerful content customization capabilities with its efficient architecture based on the Go language and support for Django template engine syntax.

2025-11-06

How to determine if a string variable is empty in AnQiCMS template?

AnQiCMS (AnQiCMS) takes advantage of the efficient and flexible template mechanism of the Go language, bringing great convenience to content management.As an experienced website operations expert, I know that the robustness of template content is crucial when building high-quality, user-friendly websites.How can you elegantly handle empty string variables in templates, which is a common detail problem we often encounter in our daily work.Properly handle these situations, not only can it avoid unnecessary blank spaces or error messages on the page, but also significantly improve the user experience.

2025-11-06

How does the `if` tag in AnQiCMS template judge whether a numeric variable is `0` or `0.0`?

How to elegantly judge whether a numeric variable is zero in Anqi CMS template?As an experienced website operations expert, I know how important it is to flexibly use the conditional judgment function of templates in daily content management.Especially when dealing with dynamic data, such as product inventory, article views, or price variables of this kind, we often need to determine whether they are "zero" or "0.0", so as to display different content or execute different logic.

2025-11-06

How to display a specific content block based on the `archiveDetail` or `categoryDetail` Id value?

As an experienced website operations expert, I am well aware that how to flexibly display specific content in content management is crucial for improving user experience and achieving operational goals.AnQiCMS (AnQiCMS) takes advantage of its powerful template engine and rich tag system, providing us with great convenience.Today, let's delve into how to cleverly use the `archiveDetail` and `categoryDetail` tag's `Id` value to accurately control the display of content blocks on the website.

2025-11-06

In AnQiCMS template, how to determine if a certain `flag` attribute (such as `recommended` or `headline`) is set?

As an experienced website operations expert, I am well aware in my daily work that the flexibility of content display is crucial for user experience and operational efficiency.AnQiCMS (AnQi Content Management System) is the choice of many enterprises and operators due to its powerful customizability and simple and efficient template engine.Today, let's delve into a very practical skill in AnQiCMS template making: how to determine if the specific `flag` attribute (such as "Recommended" or "Top") of a document is set, and adjust the content display accordingly.

2025-11-06

How to determine if there is a previous or next document on the document detail page and display navigation links?

As an experienced website operations expert, I know that a smooth reading experience is crucial when users browse content.A good document detail page should not only display the core content but also guide users to the next step, such as reading related content or easily switching to the previous or next page.This not only increases the user's stay time on the website and reduces the bounce rate, but it is also SEO-friendly, helping search engines to better crawl and understand the website structure.In AnQiCMS (AnQiCMS), implement the previous and next navigation links on the document detail page

2025-11-06

How to judge whether the website is closed and display a prompt based on the `SiteCloseTips` of the system settings (`{% system %}`)?

As an experienced website operations expert, I know that the stable operation of the website is of great importance, but sometimes, due to reasons such as maintenance, upgrades, or data migration, the website has to temporarily shut its doors.How to communicate with visitors elegantly during a closure period, ensuring user experience while effectively managing expectations, is a detail that every operator needs to pay attention to.AnQiCMS (AnQiCMS) provides a flexible mechanism in this regard, allowing us to deal with it easily.Today, let's delve deeply into how to set up tags in Anqi CMS through system settings `{% system

2025-11-06