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 pattern, such as being divisible by 3, we hope it can have a special display.It is fortunate that AnQiCMS's powerful template engine provides a simple and efficient method to achieve this requirement.This article will delve into how to flexibly determine if a number can be evenly divided by another number in AnQiCMS templates, and provide practical code examples for you based on actual scenarios.

Core Function:divisiblebyFilter

AnQiCMS's template syntax inherits from Django template engine, which includes a rich set of built-in filters (Filter),divisiblebyIt is one of the special filters used to determine the relationship of divisibility.Its function is very intuitive: check if a number (or a string that can be converted to a number) can be evenly divided by another specified number.Trueotherwise, returnFalse.

Basic Usage

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

For example, if you want to judge whether the number 21 can be divided by 3, you can write it like this:

{{ 21 | divisibleby: 3 }}

This will output:True.

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

{{ 22 | divisibleby: 3 }}

This will output:False.

Actual application scenarios and combination usage

SingledivisiblebyThe filter itself returns 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 bring out its real function.

Combineiftags for conditional judgments

The most common scenario is to decide whether to display a certain piece of content or apply a certain style based on the result of division with no remainder. For example, if you want to display a 'Special Offer' tag whenever the price of a product is a multiple of 100, you can construct the logic in this way:

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

Here are theproduct.PriceCan be a variable that passes a product price from the backend.

Apply 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 line every N articles.divisiblebyFilter and loop counter (forloop.Counter) is particularly powerful when combined.

AnQiCMS template'sforloop.CounterVariables can beforUsed in loops, it represents the current loop count, starting from 1. By using this counter, we can easily implement dynamic styles based on integer division.

For example, to implement an article list where the background of every 3 articles is highlighted, 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.Counteris divisible by 3,<li>The element will obtainhighlightThe class name, thus applying the preset highlight style.

The source of the number

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

Practical code examples

Below are some specific code examples that demonstratedivisiblebyvarious application methods of the filter.

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

Determine whether a number is odd or even is one of the simplest applications of integer division.

{% set myNumber = 15 %}

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

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

假设您有一个新闻列表,希望每偶数行的新闻标题显示为蓝色。

<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.Counter用于获取当前循环的索引,当索引是偶数时,为divAddeven-rowclass.

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

If there is a content model named in your AnQiCMSproduct_codeThe custom field is and you want to determine if the numeric part of this product code can be divided by a specific number. Assumingproduct_codeIt stores 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 the integer division judgment is performed.

Precautions

When usingdivisiblebyThere are several points to pay attention to when using the filter:

  • Data Type:The filter attempts to convert the 'checked number' and 'divisor' to numeric types for calculation. This means that even if you pass them as strings in numeric form (such as"21"It usually works as well.But to ensure the robustness of the code, it is better practice to pass actual numeric type variables as much as possible.If the value passed in cannot be converted to a number, it may lead to unexpected behavior or template rendering errors.
  • 语法准确:Filter namedivisiblebyIt is fixed and case-sensitive, and the colon and divisor following it cannot be omitted.
  • Combination with other filters:If your numbers need to be processed first (such as converting from floating-point numbers to integers), you can use other filters (such as|integeror|float) then chain them togetherdivisiblebyto ensure the accuracy of the calculation. For example{{ myFloatNumber | integer | divisibleby: 5 }}.

Pass