How to use the `filter-divisibleby` filter to determine the divisibility of a number in an `if` statement?

Calendar 👁️ 59

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"是产品模型 #}
        {%
    

Related articles

How to use the `filter-contain` filter in AnQiCMS to check string or array containment in the `if` statement?

As an experienced website operations expert, I fully understand that in an increasingly complex network environment, efficient content management and flexible page display are crucial for improving user experience and SEO effectiveness.AnQiCMS (AnQiCMS) provides a series of practical tools with its high-performance architecture based on the Go language and the powerful features of the Django template engine.

2025-11-06

How to use the `if` tag in combination with `forloop.Counter` to alternate the odd and even row styles of list items?

As an experienced website operation expert, I fully understand how to enhance user experience through detailed interface design in content presentation.AnQiCMS, with its high-performance architecture based on Go language and flexible template engine, provides us with great freedom to easily implement all kinds of creativity and features.

2025-11-06

How to elegantly handle empty list situations in AnQiCMS template using the `{% for ... empty ... %}` syntax?

In AnQiCMS template development, we often need to display a series of content lists, such as article lists, product lists, navigation menus, or friend links.However, these lists are not always filled with data. When the list is empty, how to elegantly inform the user that "there is no content here" instead of displaying a blank page or an error message has become a detail consideration in template design.AnQi CMS is well-versed in this, drawing on the excellent design of Django templates in its powerful template engine developed in Go language, providing us with `{% for ...

2025-11-06

How to use the `IsCurrent` property to mark the currently selected filter condition in the `archiveFilters` filter tag?

As an experienced website operation expert, I deeply understand the critical role of user experience (UX) in the success of a website.A user-friendly, responsive interface can effectively guide users to discover content, and the content filtering feature is an important aspect of improving user experience.In AnQiCMS (AnQiCMS) this efficient and customizable content management system, the `archiveFilters` tag provides strong support for building flexible filtering functions, and its internal `IsCurrent` property is the 'magic wand' that lights up the user experience. Today

2025-11-06

How to use the `filter-length_is` filter in AnQiCMS templates for `if` judgment of the exact length of a collection?

## Precise Control and Intelligent Presentation: Efficient Application of the `filter-length_is` Filter in AnQiCMS Templates In the template development practice of AnQiCMS, we often need to finely control the displayed data to ensure the accurate transmission of content and the elegant presentation of the user interface.AnQi CMS provides powerful tools for content operators and developers with its efficient architecture based on the Go language and the flexible syntax of the Django template engine, among which

2025-11-06

What is the difference in handling variable null values between `filter-default` and `filter-default_if_none` in `if` condition judgments?

AnQiCMS (AnQiCMS) is a powerful assistant for our daily content operation, with its flexible template engine syntax making content display diverse and efficient.However, in practice, we often encounter situations where variable values are empty, such as when the article title is not filled in, or when a custom field has no data.How elegantly to handle these 'null' values to ensure that the website front-end display is always professional and friendly has become a topic that content operators must face.

2025-11-06

How does the `filter-yesno` filter help AnQiCMS templates handle the tri-state logic of 'yes/no/unknown'?

As an experienced website operations expert, I know that how to present data status efficiently and clearly in a content management system is one of the keys to successful operations.AnQiCMS (AnQiCMS) provides an excellent solution in this aspect with its flexible template engine and rich built-in filters.Today, let's delve into a seemingly simple yet powerful filter——`filter-yesno`, and how it helps AnQiCMS templates handle complex ternary logic such as 'yes/no/unknown'.

2025-11-06

How to load different layouts in AnQiCMS templates based on the current template type (such as `template_type`)?

In the practice of AnQiCMS (AnQiCMS), flexible template management is a key link in the efficient operation of the website.As an experienced website operations expert, I am well aware of how to load the appropriate layout for different scenarios, which not only optimizes user experience but also enhances the website's response speed and maintenance efficiency.Today, let's delve deeply into how to cleverly load different layout strategies in the AnQiCMS template, such as `template_type`.

2025-11-06