How to check if a number is divisible by another number in AnQiCMS template?

Calendar 👁️ 62

During the development of AnQiCMS templates, we often encounter situations where we need to adjust the display of content or apply different styles based on specific conditions.For example, when a number in the list meets a certain rule, such as being divisible by 3, we hope it can have a special appearance.luckyly, the powerful template engine of AnQiCMS provides a simple and efficient way to meet this requirement.This article will delve into how to flexibly determine whether one number can be evenly divided by another in the AnQiCMS template, and provide practical code examples combined with actual scenarios.

Core function:divisiblebyFilter

AnQiCMS's template syntax borrows from the Django template engine, which includes a wealth of built-in filters (Filter),divisiblebyIt is one of the special filters used to determine the divisibility relationship.Its function is very intuitive: to check if a number (or a string that can be converted to a number) can be divided by another specified number.If it can be divided evenly, it will returnTrue; otherwise, it returnsFalse.

Basic usage

UsedivisiblebyThe syntax of the filter is very concise, usually presented like this:{{ 被检查的数字 | divisibleby: 整除数 }}.

For example, to determine if the number 21 is divisible by 3, you can write it like this:

{{ 21 | divisibleby: 3 }}

This will outputTrue.

If you want to judge whether the number 22 can be divided by 3, you can write it like this:

{{ 22 | divisibleby: 3 }}

This will outputFalse.

Actual application scenarios and combination usage

SingledivisiblebyThe filter itself will return a boolean value, but in actual template development, we usually combine it with logical judgment tags (such as{% if %}) or loop tags (such as{% for %}) to exert its true effect.

Combineiftag for conditional judgment

The most common scenario is to determine whether to display certain content or apply a style based on the result of division. For example, if you want to display a "Special Offer" tag every time a product price is a multiple of 100, you can build the logic like this:

{% if product.Price | divisibleby: 100 %}
  <span>特惠商品!</span>
{% endif %}

Hereproduct.PriceCan be a product price variable passed from the backend.

Applied in a loop to achieve dynamic styling of list elements.

Imagine you have a list of articles, and you want to change the background color of the articles every few lines, or add a special separator every N articles. At this point,divisiblebyFilter and loop counter (forloop.Counter) combination is especially powerful.

AnQiCMS template inforloop.CounterVariables can beforIn a loop, it represents the current loop count, starting from 1. Using this counter, we can easily implement dynamic styles based on division relations.

For example, to implement a list of articles where every third article has a highlighted background effect, you can do it like this:

<ul class="article-list">
    {% for item in archives %}
        <li class="article-item {% if forloop.Counter | divisibleby: 3 %}highlight{% endif %}">
            <h3>{{ item.Title }}</h3>
            <p>{{ item.Description }}</p>
        </li>
    {% endfor %}
</ul>

By this method, whenforloop.Counterit is divisible by 3,<li>The element will obtainhighlightThe class name is applied to highlight the preset style.

The source of the number

used fordivisiblebyThe number checked by the filter can come from various sources, such as hard-coded numbers directly written in the template, or variables passed from the backend (such asarchive.Id/item.Views),even it can be a custom field value in the background of the user. It is important that as long as the value can be identified and converted to a number for calculation by the template engine,divisiblebythe filter can work normally.

Practical code examples

Here are some specific code examples that demonstratedivisiblebyvarious applications of filters.

Example one: Determine the parity of a number (basic application)

Determine whether a number is odd or even, which is one of the simplest applications of divisibility判断。

{% set myNumber = 15 %}

<p>数字 {{ myNumber }} 是
{% if myNumber | divisibleby: 2 %}
  偶数。
{% else %}
  奇数。
{% endif %}
</p>

Example two: Implement alternating row coloring in a list (loop application)

Suppose you have a news list and you want the news titles on every even line to be displayed in blue.

<style>
    .news-item.even-row h3 { color: blue; }
</style>

<div class="news-section">
    {% archiveList newsArticles with type="list" limit="10" %}
        {% for article in newsArticles %}
            <div class="news-item {% if forloop.Counter | divisibleby: 2 %}even-row{% endif %}">
                <h3><a href="{{ article.Link }}">{{ article.Title }}</a></h3>
                <p>{{ article.Description | truncatechars: 100 }}</p>
            </div>
        {% endfor %}
    {% endarchiveList %}
</div>

In this example,forloop.CounterUsed to get the current loop index, when the index is even, it isdivaddeven-rowClass.

Example three: Check if the value of the custom field is divisible

If your AnQiCMS content model has a field namedproduct_codeThe custom field, and you want to determine whether the numeric part of this product code can be evenly divided by a specific number. Assumeproduct_codeStored is pure numbers.

{% archiveDetail productDetail with name="product_code" %}

<p>产品代码:{{ productDetail }}</p>

{% if productDetail | integer | divisibleby: 7 %}
  <p>这个产品代码是幸运数字!</p>
{% else %}
  <p>这个产品代码并非幸运数字。</p>
{% endif %}

Here we first go through|integerThe filter ensuresproductDetailTreated as an integer, then perform integer division.

Points to note

While usingdivisiblebyWhen filtering, there are several points to note:

  • Data type:The filter will try to convert the 'checked number' and 'divisor' to numeric types for calculation. This means that even if you enter the number in string format (such as"21"It usually works fine. But to ensure the robustness of the code, it is better practice to pass actual numeric type variables whenever possible.If the value passed cannot be converted to a number, it may lead to unexpected behavior or template rendering errors.
  • The syntax is correct:Filter namedivisiblebyIt is fixed and case-sensitive, and the colon and divisor that follows cannot be omitted either.
  • Combined with other filters:If your number needs to be processed by other operations first (such as converting from a floating-point number to an integer), you can first use other filters (such as|integeror|float) and then chain themdivisiblebyto ensure the accuracy of the calculation. For example{{ myFloatNumber | integer | divisibleby: 5 }}.

By

Related articles

What is the difference between the `default` and `default_if_none` filters for handling null values in AnQiCMS?

In AnQiCMS template development, we often need to handle the case where data may be empty to ensure the stability of page display and user experience.AnQiCMS provides rich template filters to meet such needs, where `default` and `default_if_none` are very practical tools for handling null values.They are both intended to provide a fallback value for missing or empty data, but there is a subtle yet critical difference in the definition of 'empty,' understanding these differences can help us more precisely control the template rendering logic.###

2025-11-08

How to set a default display value for possibly empty variables in AnQiCMS templates?

When using AnQiCMS to build a website, we often encounter such situations: some content fields may not have values every time, such as articles may not have thumbnails, products may not have detailed descriptions, or some contact information may not have been filled in.If variables that may be empty are directly called in the template, ugly blank spaces may appear on the page, or even program errors, which will greatly affect the user experience and the professionalism of the website.Luckyly, the AnQiCMS template system is based on syntax similar to Django and Blade, providing a powerful and flexible mechanism to handle these situations

2025-11-08

Which date and time formatting parameters are supported by the `date` filter in AnQiCMS?

In website content management, the accurate display of dates and times is crucial for improving user experience and ensuring the timeliness of information.AnQiCMS provides a flexible and powerful template engine, allowing you to easily customize the display of dates and times on web pages.Among many practical tools, the `date` filter is an important member for handling date and time formatting.### `date` filter: A powerful tool for formatting date and time The `date` filter in AnQiCMS templates is used to format `time.Time`

2025-11-08

How to format a `time.Time` object into a specified date string in AnQiCMS template?

In website content operation, the way dates and times are presented often directly affects user experience and the clarity of information.A professional, readable date format can not only enhance the credibility of the content, but also allow visitors to obtain key information faster.AnQiCMS as a powerful content management system naturally also provides a flexible way to handle and format date and time.When we need to display the publication time, update time, or any other date information in the AnQiCMS template, we may encounter a problem

2025-11-08

What type of value does the `divisibleby` filter in AnQiCMS return in mathematical operations?

In AnQiCMS template creation, we often need to control the display of content based on some mathematical logic, such as determining whether a number can be evenly divided by another number.At this point, the `divisibleby` filter is particularly important.It helps us easily implement this judgment in the template without writing complex logic code.The `divisibleby` filter returns a very intuitive and easy-to-understand boolean value when performing mathematical integer division.This means that the result can only be two possibilities: `True` (true) or

2025-11-08

How can you view the internal structure and value of a variable in AnQiCMS templates for debugging?

During the development and content operation of Anqi CMS templates, a deep understanding of the internal structure and specific values of variables in the templates is the key to efficient debugging.When you are faced with abnormal data displayed on a page or are unsure about the available properties of an object returned by a certain tag, being able to quickly view the detailed information of variables will undoubtedly greatly enhance the efficiency of solving problems. AnQi CMS provides a flexible and powerful template engine, which draws on the syntax of Django templates, and also includes some very practical debugging tools, allowing you to directly check variables in template files. Below

2025-11-08

The `dump` filter has what help for understanding complex data structures in AnQiCMS template development?

During the template development process of AnQi CMS, we often need to deal with various data passed from the backend.AnQiCMS is a powerful content model with flexible tag system, which allows us to easily obtain articles, categories, pages, and even custom fields of data.However, when the data structure becomes complex, or when we are unsure of what content a variable contains, the efficiency of development debugging will be greatly reduced.At this moment, the `dump` filter acts like a powerful 'data perspective mirror', which can help us clearly understand these complex data structures

2025-11-08

How does the AnQiCMS template escape special characters in HTML or JavaScript code to prevent XSS attacks?

When building a website with AnQiCMS, we often need to fill dynamic content into the page template, which includes text that may come from user input.However, if not handled properly, the content entered by these users may be exploited by malicious attackers to plant malicious scripts, thereby triggering cross-site scripting (XSS) attacks.XSS attacks can steal user data, tamper with page content, even hijack user sessions, causing serious harm to websites and users.AnQiCMS as a content management system that focuses on security

2025-11-08