How to determine if a number (such as article ID) can be evenly divided by a specific number in the AnQiCMS template?

Calendar 👁️ 57

In website content display, we often encounter some special requirements, such as hoping to apply different styles to specific elements in the list, or to make some distinctions based on the parity of the article ID.AnQiCMS is a powerful template engine that draws inspiration from the excellent design of Django templates, providing a concise and efficient way to handle these logic.Today, let's discuss a very practical feature in templates: how to determine if a number (such as an article ID) can be evenly divided by a specific number.

The 'Division Check' filter in AnQiCMS template:divisibleby

The template system of AnQiCMS provides a very practical filter nameddivisibleby. This filter is specifically used to check if a number can be divided by another number.

Its basic syntax is very intuitive:

{{ 待检查的数字 | divisibleby: 目标数值 }}

This filter will return a boolean value (TrueorFalse). If the 'number to be checked' can be divided by the 'target value', it will returnTrue; otherwise, it returnsFalse.

For example, if we want to know a number10whether it can be2divided evenly, we can write it as:

{{ 10 | divisibleby: 2 }}

The output of this code will beTrue. If we were to10replace11, and the result will beFalse.

Actual application scenarios and code examples

UnderstooddivisiblebyAfter the use of the filter, let's see how it plays a role in the AnQiCMS template.

Scenario one: apply different styles to specific elements in the list

Imagine, in an article list, you want to give a special background color to every third article, or insert a separator at the end of each line.This is a very common and useful technique in front-end design.

at AnQiCMS's{% for %}Within a loop, besides accessing the properties of the current loop item (such asitem.Id/item.Title), we can also useforloop.Counterto get the index of the current loop (starting from1). Useforloop.CounterPerforming division check can help us accurately control the display order of elements on the page.

Here is a code example that shows how to add a special to every third article in an article listspecial-styleClass:

{% archiveList archives with type="list" limit="10" %}
    {% for item in archives %}
        <div class="article-item {% if forloop.Counter | divisibleby: 3 %}special-style{% endif %}">
            <a href="{{ item.Link }}">{{ item.Title }}</a>
            <p>{{ item.Description|truncatechars:50 }}</p>
            <span>文章ID: {{ item.Id }},当前排序: {{ forloop.Counter }}</span>
        </div>
        {# 如果希望在每第三篇文章后插入一个分隔符,可以这样: #}
        {% if forloop.Counter | divisibleby: 3 %}
            <div class="divider">---</div>
        {% endif %}
    {% endfor %}
{% endarchiveList %}

In this code block,forloop.Counter | divisibleby: 3It will judge whether the display number of the current article in the list is3a multiple. If it is, it will givedivan element plusspecial-stylea class, so that you can define its special style through CSS.

Scenario two: Distinguish according to the parity of the article ID.

A common requirement is to differentiate the display style based on the parity of the article or product ID, for example, odd ID articles are displayed to the left, even ID articles are displayed to the right, or have different background colors.This helps create a more dynamic page layout.

In this case, we need to use the article'sIdattribute and determine if it can be2divided evenly.

{% archiveList archives with type="list" limit="10" %}
    {% for item in archives %}
        <div class="article-item {% if item.Id | divisibleby: 2 %}even-id-style{% else %}odd-id-style{% endif %}">
            <h3><a href="{{ item.Link }}">{{ item.Title }}</a></h3>
            <p>文章ID: {{ item.Id }}</p>
            <p>{{ item.Description|truncatechars:50 }}</p>
        </div>
    {% endfor %}
{% endarchiveList %}

here,item.Id | divisibleby: 2It will directly check the article's database ID. IfIdit is even, thendivisibleby: 2ReturnTrueelements gainedeven-id-styleelse gainodd-id-style.

more creative applications

The flexibility of this integer division judgment is not limited to this. You can use it to create more creativity:

  • special content for specific IDs:For example, if a certain categoryIdIs5displays a special category description.
  • Group display:In a complex list of products, eachNproduct is grouped usingdivisiblebyTo determine the start or end of a group, add a group title or style.
  • Content trigger:Determine whether a numerical property (such as product inventory, user points) has reached a certain multiple, triggering specific prompts or discount information.

Notes and tips

  1. Ensure the variable is of numeric type: divisiblebyThe filter expects a number as input. In AnQiCMSId/forloop.CounterDirectly use built-in numeric variables. If it is a custom field, make sure the data type is set to numeric.
  2. withifTag combination: divisiblebyThe filter returns a boolean valueTrueorFalseTherefore, it usually needs to be used with the AnQiCMS template,{% if %}combined with logical judgment tags to bring out its function, achieving conditional content display or style modification.
  3. item.Idvsforloop.Counter:
    • Useitem.Id | divisibleby: NIt is determined by the real and unique numeric identifier recorded in the database.This means that even if the list order changes, the same ID article will still receive the same judgment result.
    • Useforloop.Counter | divisibleby: NIt is based on the current article inthe order of the rendering list on the pageTo judge. This means that the result is related to the pagination, sorting, and other display logic of the page. Which one to choose depends on your specific needs.

Summary

BydivisiblebyFilter, AnQiCMS templates provide website developers and operators with simple and powerful tools, allowing easy implementation of conditional judgments based on digital division.Whether it is to optimize the display of the list, implement odd and even row styles, or build more complex dynamic content layouts, this little filter can help you achieve your goals in an efficient and elegant way.Fully utilize the flexibility of the AnQiCMS template to make your website content more attractive!


Frequently Asked Questions (FAQ)

1.divisiblebyCan the filter only be used for article ID or list number?

No,divisiblebyThe filter can be used for any numeric variable. Other than article ID (item.Id) and loop number (forloop.CounterIn addition, you can also apply it to category IDs, user IDs, or any numeric fields you create in your custom models in the background.If the value of the variable is a number, you can use this filter to make a judgment.

How can I determine if a number cannot be divided by a specific value?

You can combine{% if not %}The syntax to implement the judgment of "not divisible". For example, if you want to find articles that cannot be3divided by:

{% if not item.Id | divisibleby: 3 %}
    {# 此处的代码将在文章 ID 不能被 3 整除时执行 #}
{% endif %}

3.divisiblebyCan it be used for string values? For example, if my article ID is a string like '123'?

TheoreticallydivisiblebyThe filter expects to receive a number. In some cases, the AnQiCMS template engine will automatically try to convert strings that look like numbers to

Related articles

How to concatenate array elements obtained by traversing AnQiCMS templates with a specified separator into a string?

During the template creation process in Anqi CMS, we often encounter the need to concatenate a certain field from an array (or list) queried from the database using a specific symbol to form a continuous string for beautiful display on the page, such as connecting multiple tags (Tag) of an article or displaying all the characteristics of a product.The Anq CMS template engine supports syntax similar to Django templates, making it intuitive and flexible to handle such requirements.The core idea is to use the loop structure of the template engine to traverse the array

2025-11-07

How to quickly split a comma-separated string into an array for traversal in AnQiCMS template?

In website content operation, we often encounter such situations: a content field stores a series of interconnected information, usually connected by commas.For example, an article may be associated with multiple tags ("SEO, website optimization, content marketing"), a product may have various color options ("red, blue, green"), or you may need to display a set of user-defined keywords.How to efficiently convert the comma-separated strings to traversable data structures when we need to display them one by one on the front-end page or perform more complex processing

2025-11-07

Why does the AnQiCMS template default to escaping HTML code? How can HTML content be safely output?

When using AnQiCMS for template development, we may notice an interesting phenomenon: sometimes, the HTML code directly output in the template, such as a `<div>` tag, is not parsed by the browser into a visible area as expected, but is displayed exactly as it is, showing `&lt;div &gt; `such characters. This may be confusing, why does AnQiCMS default to escaping HTML code?How can we safely output the HTML content we want?--- ### One

2025-11-07

How to control the truncation length of the hyperlink text when using the AnQiCMS `urlizetrunc` filter?

In website content operation, we often need to display various hyperlinks in articles, comments, or list pages.These links may be pointing to other content within the site, external resources, or the contact email of the user.However, some excessively long links may not only destroy the page layout, affect the aesthetics, but may also reduce the user's reading experience.Especially in limited display space, long URLs can make content look disorganized.

2025-11-07

How to concatenate or add two strings or numbers in AnQiCMS template?

In AnQi CMS template design, dynamically combining text information or performing calculations on numerical values is a common requirement.It is crucial to be able to handle string concatenation and numeric addition operations flexibly, whether it is to build personalized product descriptions or to display dynamic data on the page.The Anqi CMS template system, drawing on the syntax of the Django template engine, provides a variety of intuitive and powerful methods to complete these tasks.

2025-11-07

How to implement the `replace` filter of AnQiCMS for batch replacement of sensitive words in article content?

In the field of content management, the flexibility and maintainability of website content are crucial.Batch replacement of article content is an efficient and practical operation, whether it is for brand unification, information update, or sensitive word filtering.AnQiCMS as a rich-featured enterprise-level content management system, provides a variety of content processing mechanisms, among which the `replace` filter and the background content batch replacement feature play a key role in different scenarios.

2025-11-07

How to automatically render the multi-line text (including newline characters) of AnQiCMS custom fields into HTML tags `<p>` and `<br/>`?

When using AnQiCMS to manage website content, we often take advantage of its powerful custom field features to enrich page information.Especially for text that needs to include multiline descriptions, notes, or detailed explanations, the 'Multiline Text' type in custom fields is undoubtedly the ideal choice.However, when we eagerly display these text contents with line breaks on the front-end page, we may find that the original line breaks are not automatically recognized by the browser, resulting in all the text being cramped together, greatly reducing the reading experience.This is actually a feature of HTML rendering mechanism

2025-11-07

How to automatically add line numbers to plain text content entered in the background of AnQiCMS template?

When operating a website, we often encounter such a need: we hope that the pure text content entered in the background can automatically display line numbers on the front end, which is very helpful for displaying code, poetry, configuration scripts, or any text content that needs to be read line by line.Although AnQiCMS back-end content entry is very convenient, how can these plain text contents be automatically numbered with beautiful line numbers when displayed on the front end?The flexible template engine of Anqi CMS can help us easily achieve this.### Understanding the Source and Processing Basics Firstly, we need to clarify the source of this plain text content

2025-11-07