In AnQiCMS template development, achieving dynamic content display and fine-grained control is a daily challenge for website operation experts.AnQiCMS is highly praised for its concise and efficient template engine, which inherits the elegant style of Django templates, making content display and logical control intuitive.In website operation, we often need to present different content or styles based on the characteristics of numbers, such as displaying an advertisement every few products, or setting different background colors for odd and even rows of the list. At this time,divisiblebyThe filter can beifCombined with statements, it helps us easily meet these needs.

UnderstandingdivisiblebyFilter: Determines the divisibility of a number

First, let's delve deeper intodivisiblebyFilter. It is a very practical tool in the AnQiCMS template system. As the name implies, its main function is to determine whether a number can be evenly divided by another number, i.e., there is no remainder.When a number can be divided by another number,divisiblebywill returnTrue(true); otherwise, returnFalse(false).

Its basic usage is quite simple: you just need to put the number to be judged in the pipe symbol|before, then add after the pipe symboldivisibleby:除数it is. For example,{{ 21|divisibleby:3 }}will returnTruebecause 21 can be divided by 3; and{{ 22|divisibleby:3 }}it will returnFalseBecause 22 divided by 3 has a remainder. Even if the number is in string form, such as"21",AnQiCMS's template engine can also intelligently perform type conversion for correct judgment.

todivisiblebythe filter meetsifcombined statement usage

It is not enough to simply judge whether a number is divisible, we need to execute different template logic based on the judgment result. At this point, we need to introduce the powerful template in AnQiCMS.ifLogical judgment label.

ifA statement allows us to decide whether to render a section of template content based on the truth value of a condition. WhendivisiblebyThe filter returnsTruethen,ifThe code block within the statement will be executed; if it returnsFalse, thenifThe code block will be skipped and the execution will proceed.else(If it exists) a code block.

todivisiblebyNested in a filter.ifIn the statement, the syntax structure is roughly as follows:

{% if 待判断的数字 | divisibleby:除数 %}
    <!-- 当数字可被除尽时显示的内容 -->
{% else %}
    <!-- 当数字不可被除尽时显示的内容 -->
{% endif %}

This combination method brings great flexibility and readability to template logic.

Example of practical application scenarios

Let's look at some specific examples to see.divisiblebyHow does the filter work in the AnQiCMS template.

Example one: Display special content every N elements in the loop.

In content list display, we may want to insert a special style or prompt every fixed number of contents.Assume we have a list of articles and want every third article to have a special marker.

<div class="article-list">
    {% archiveList archives with type="list" limit="10" %}
        {% for item in archives %}
            {# forloop.Counter 表示当前循环的次数,从1开始 #}
            <div class="article-item {% if forloop.Counter|divisibleby:3 %}highlight-item{% endif %}">
                <h3><a href="{{ item.Link }}">{{ item.Title }}</a></h3>
                <p>{{ item.Description|truncatechars:100 }}</p>
                {% if forloop.Counter|divisibleby:3 %}
                    <span class="special-tag">⭐ 精选推荐 ⭐</span>
                {% endif %}
            </div>
        {% endfor %}
    {% endarchiveList %}
</div>

Here, forloop.CounterIt is a very useful built-in variable in the AnQiCMS template loop, representing the current loop count starting from 1. Whenforloop.Counterit is divisible by 3, we aredivthe taghighlight-itemThe class name is displayed with a special recommendation tag. This way, every third article is highlighted, cleverly breaking the monotony of the list.

Example two: alternating row style switch (zebra line effect)

Another common scenario is to set different background colors for odd and even rows of tables or lists to enhance readability, which is what we often call the 'zebra line' effect. This can also be achieved bydivisiblebyThe filter can be easily implemented.

`twig

    {% archiveList products with type="list" limit="10" moduleId="2" %} {# 假设moduleId="2"是产品模型 #}
        {%