How to use the `divisibleby` filter in AnQi CMS template to achieve alternate row coloring or group output every N elements?

Calendar 👁️ 64

In website operation, a carefully designed page layout and content presentation can significantly improve user experience and readability.Especially in scenarios with a large number of items in a list, if all entries are presented in the same style, it is easy to cause visual fatigue.At this time, changing rows by color or grouping by a specific quantity can well solve these problems.The template engine of AnQiCMS (AnQiCMS), with its flexible Django template syntax, provides powerful filter functions, wheredivisiblebyThe filter is a tool to meet such needs.

This article will delve into how to巧妙运用安企CMS templates.divisiblebyFilter, implement list alternating color or group output by N elements in a loop.


UnderstandingdivisiblebyFilter

The Anqi CMS template engine supports syntax similar to Django, including many practical filters (filters).divisiblebyThe filter, as the name implies, is used to determine whether one number can be evenly divided by another.It returns a boolean value (True or False), making it an ideal tool for conditional judgment in loops.

Its basic usage is very intuitive:

{{ 某个数字 | divisibleby: 另一个数字 }}

For example,{{ 21 | divisibleby:3 }}It will returnTrueBecause 21 is divisible by 3.{{ 22 | divisibleby:3 }}it will returnFalse.

In the loop of the template, we often useforloop.Counterthis special variable. It represents the current loop index, starting from 1. Combinedforloop.CounteranddivisiblebyWe can easily control the display logic of list elements.

Implement alternating row coloring of the list

The use of alternating colors for rows is one of the most common methods to enhance the readability of a list.It applies different background colors to the odd and even rows in the list, making it easier for users to distinguish and read each item.

Operation ideas

To implement alternating row coloring, we need to determine whether the current loop index is odd or even. Whenforloop.Counterit is divisible by 2, it indicates that the current row is even; otherwise, it is odd.

Example Code

Suppose we have a list of documents, and we want to display different background colors for alternating rows.

First, define two classes in your CSS style file, for example.odd-rowandeven-row:

/* public/static/css/style.css 或您的自定义CSS文件 */
.article-list li {
    padding: 10px;
    margin-bottom: 5px;
}
.odd-row {
    background-color: #f9f9f9; /* 浅灰色背景 */
}
.even-row {
    background-color: #e0e0e0; /* 稍深一点的灰色背景 */
}

Continue, in the Anqi CMS template file (for examplearchive/list.htmlUse it indivisiblebyThe filter dynamically adds style classes to list items:

<ul class="article-list">
    {% archiveList archives with type="page" limit="10" %}
        {% for item in archives %}
            <li class="{% if forloop.Counter|divisibleby:2 %}even-row{% else %}odd-row{% endif %}">
                <!-- 这里是您的文章内容 -->
                <h3><a href="{{ item.Link }}">{{ item.Title }}</a></h3>
                <p>{{ item.Description|truncatechars:100 }}</p>
                <span>发布日期:{{ stampToDate(item.CreatedTime, "2006-01-02") }}</span>
            </li>
        {% empty %}
            <li>暂无相关内容。</li>
        {% endfor %}
    {% endarchiveList %}
</ul>

In this example,forloop.Counter|divisibleby:2It will judge whether the current row is an even row. If it is, applyeven-rowthe class; otherwise, applyodd-rowClass. In this way, we easily achieved the alternating row color effect of the list.

Tip:If it is just to achieve a simple alternating row color, Anqi CMS template also provides a simpler one.cycleLabel. For example:

<li class="{% cycle 'odd-row' 'even-row' %}">
    <!-- 内容 -->
</li>

However, `divisibleby

Related articles

How to use the `length_is` filter in an Anqie CMS template to validate the length of user input or data lists and return a boolean value for conditional rendering?

In Anqi CMS template development, flexibly controlling the display method of content is the key to improving website user experience.Among them, the `length_is` filter is a very practical tool that can help us easily check the length of user input or data lists in templates and conditionally render based on the check results.The core function of the `length_is` filter is to determine whether the length of a variable (whether it is a string, array, or mapping) is equal to a predefined value.It does not directly return the specific length value

2025-11-08

The `length` filter calculates the length of Chinese string in an AnQi CMS template, is it counted by bytes or characters? How does it affect the output?

In AnQi CMS template development, dealing with string length is a common requirement.When dealing with multilingual content, especially when it includes Chinese characters, how a string's length is calculated in bytes or characters can directly affect the accuracy of template output and the presentation of the content.The `length` filter used in the Anqi CMS template is designed to solve this problem.It counts the length of the string by characters rather than by bytes.This means, whether it is an English letter

2025-11-08

How to use the `get_digit` filter to extract a specific digit from a long numeric ID and display or perform logical operations in an Anqi CMS template?

In the development of Anqi CMS templates, we sometimes encounter situations where we need to extract a certain number of digits from a long numeric ID.For example, you may want to assign different styles to content based on some number of the ID, or make some logical judgments.At this time, the `get_digit` filter can be used.It provides a concise and efficient way to achieve this goal, making your template logic more flexible.

2025-11-08

In the Anqi CMS template, what is the difference between the `default` and `default_if_none` filters when the variable is `nil` or empty?

During the template creation process of AnQi CMS, flexibly using variables and their default values is the key to improving the robustness and user experience of the template.When you are dealing with a variable that may not exist (`nil`) or is empty, the `default` and `default_if_none` filters are the tools you commonly use.Although they can all provide an alternative output when the variable 'no value' occurs, their criteria for determining 'no value' are subtle and important.To understand the difference between the two

2025-11-08

In addition to `stampToDate`, how can the `date` filter of Anqi CMS implement custom date and time formatting when handling `time.Time` type?

In Anqi CMS, the display of dates and times often needs to be adjusted flexibly according to the actual needs of the website.We all know that the `stampToDate` filter is very convenient for handling Unix timestamps, as it can easily convert a string of numeric timestamps into the date format we need.But sometimes, the date value we handle in the template may already be a `time.Time` type object in Go language, rather than the original timestamp.In this case, Anqi CMS provides another powerful tool, that is `date`

2025-11-08

How to format Unix timestamp to 'YYYY-MM-DD HH:MM' and other localized date strings using the `stampToDate` filter in AnQi CMS?

In website content management, the display of date and time information is ubiquitous.Whether it is the publication time of the article, the listing date of the product, or the submission time of the comments, a clear and readable date format is crucial for improving user experience.AnQiCMS (AnQiCMS) fully understands this need and provides powerful template tags and filters, among which the `stampToDate` filter is a powerful tool to convert the original Unix timestamp into a localized date string that we are familiar with.This article will delve into the usage of the `stampToDate` filter

2025-11-08

How to safely use the `truncatechars_html` filter in Anqi CMS template to truncate HTML rich text content and automatically close tags?

In website content operation, we often need to display the partial content of articles, products, or single pages on list pages, abstract areas, or specific blocks.This content is often rich text that includes HTML tags. Simply truncating by character count may cause the HTML tags to be cut off, thereby破坏页面布局和显示效果.For example, a `<p>This is a paragraph<strong id=“test”>bold</span>` content that is abruptly truncated may cause the page to display unclosed tags.

2025-11-08

What are the differences in the truncation logic of the `truncatewords` and `truncatechars` filters when truncating the abstract of an AnQi CMS article?

In Anqi CMS, in order to display the article summary on the list page or preview area, we often need to truncate the article content.At this time, the `truncatewords` and `truncatechars` filters come into play.They all can help us to shorten long content, but there are significant differences in the truncation logic between them, especially in handling Chinese and English characters and words, where their performance is even more disparate.Understanding these differences can help us better control the presentation of the summary.

2025-11-08