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