In AnQiCMS template, how to determine if one number can be evenly divided by another to achieve conditional display?

In Anqi CMS template development, we often need to display content based on specific conditions, such as adding a special style every few elements in a list or inserting separators at specific positions.When this condition is to judge whether a number can be evenly divided by another number, the powerful template engine of Anqi CMS provides a concise and efficient solution.

The AnQi CMS template system adopts syntax similar to the Django template engine, making it very intuitive to handle such logical judgments. To determine if one number can be evenly divided by another, we mainly use the 'filter' function in the template, especiallydivisiblebyfilter.

Core function:divisiblebyFilter

divisiblebyThe filter is designed specifically to check the divisibility of numbers. Its function is to determine whether a number can be evenly divided by another number and returns a boolean value (True or False).This filter greatly simplifies the logic we use to make such judgments in the template.

Its basic usage is very intuitive:

{{ 待判断的数字 | divisibleby: 除数 }}

For example, if you want to know if a number10can be divided by2divisible, you can write it like this:

{{ 10 | divisibleby: 2 }}

This will outputTrue. If the number is11then, it will output.False.

combined with conditional judgment to achieve flexible display

Knowing whether a number is divisible is not enough, what we need more is to use this result to achieve conditional content display. At this point, it is necessary to combine the conditional judgment tags in the Anqi CMS template.{% if %}.

Suppose we have a list of articles.archivesWe hope to insert a special ad position or clear the float after displaying three articles. This is very useful when creating a multi-column layout. We can take advantage offorthe built-in loop.forloop.CounterA variable that represents the current loop index, starting from 1.

Below is an example of inserting an element in an article list, after displaying three articles.divAn example of an element to clear the float:

<div class="article-list">
    {% for article in archives %}
        <div class="article-item">
            <h3><a href="{{ article.Link }}">{{ article.Title }}</a></h3>
            <p>{{ article.Description }}</p>
        </div>

        {# 判断当前文章序号是否能被3整除,如果是,则插入清除浮动的div #}
        {% if forloop.Counter | divisibleby: 3 %}
            <div class="clearfix"></div> {# 用于清除浮动或插入特定内容 #}
        {% endif %}
    {% endfor %}
</div>

In this example,forloop.Counter | divisibleby: 3It willforloop.CounterReturn when it is a number like 3, 6, 9, etc.Truethus triggeringdiv.clearfixdisplay.

Another common scenario is to apply different styles to odd and even rows in a list to improve readability or visual effects. This can also be achieved bydivisiblebyTo implement a filter:

<ul class="product-list">
    {% for product in products %}
        <li class="product-item {% if forloop.Counter | divisibleby: 2 %}even{% else %}odd{% endif %}">
            <img src="{{ product.Thumb }}" alt="{{ product.Title }}">
            <h4>{{ product.Title }}</h4>
        </li>
    {% endfor %}
</ul>

Here, whenforloop.CounterWhen the number is even (2, 4, 6...), it will addeventhe class; when the number is odd (1, 3, 5...), then addoddClass.

modulus operator%as an alternative option

exceptdivisiblebyFilter, you can also use the traditional modulus operator%to make the same judgment. The modulus operator returns the remainder after dividing two numbers. If the remainder is0, it means that it can be divided evenly.

For example:

{# 使用模运算符判断10是否能被2整除 #}
{% if 10 % 2 == 0 %}
    <p>10能被2整除</p>
{% endif %}

In terms of function,divisiblebyFilters andX % Y == 0the judgment is equivalent. However, in the template,divisiblebyFilters are generally considered more readable because they directly express the intention of 'whether it is divisible', and the code looks simpler.

Expansion of practical application scenarios

This divisibility judgment logic is widely used in website operations and template design:

  • Grid layout adjustmentCreate responsive layouts by dynamically inserting clearing floats or line-end markers based on the number of elements displayed per row (for example, 4 elements per row on PC and 2 elements per row on mobile).
  • Content grouping displayFor example, after every 5 news content, insert a 'View More' link or switch to a new display module.
  • Special promotion or event displayIn the product list, every Nth product displays a 'Time-limited discount' or 'Hot' tag.
  • Page style optimizationIn some custom pagination components, it may be necessary to adjust the style or display content based on the parity or a special divisibility rule of the current page number.

Bydivisiblebythe filter meetsifThe combination of tags, the template engine of AnQi CMS provides great flexibility for website developers, allowing them to easily implement various conditional display requirements based on numerical order, thereby creating a more dynamic and attractive user interface.


Frequently Asked Questions (FAQ)

1.divisiblebyIs the filter only usable for integers?

divisiblebyThe filter is primarily designed to determine the divisibility relationship between integers. Although it may accept floating-point numbers at the lower level, in most cases, the semantics of divisibility judgment occur within the integer range, such as judgment10Can be divided by3not divisible by10.5Can be divided by2.5. When using it, it is recommended to ensure that the number to be judged and the divisor are both integer types to avoid unexpected results.

2. How can I check if a number 'cannot' be divided by another number?

You can simply in{% if %}Add before the statementnotkeywords. For example, if you want toforin the loop, whenforloop.CounterCannot be divided by2Perform some operations when divisible (i.e., odd lines), it can be written like this:

{% if not (forloop.Counter | divisibleby: 2) %}
    {# 这是奇数行才会显示的内容 #}
{% endif %}

3. Can I check multiple divisibility conditions in the same conditional statement?

Can be. Anqi CMS template engine supportsandandorLogical operators, you can use in{% if %}Combine multiple statementsdivisiblebyFilters or other conditions. For example, if you want wheneverforloop.CounterCan be3Divisible and can be divided by5Perform operation when divisible:

{% if (forloop.Counter | divisibleby: 3) and (forloop.Counter | divisibleby: 5) %}
    {# 当序号是15、30等数字时显示 #}
{% endif %}

This makes the conditional judgment of the template more flexible in complex layout and content display logic.