In website content display, we often encounter some special requirements, such as hoping to apply different styles to specific elements in the list, or to make some distinctions based on the parity of the article ID.AnQiCMS is a powerful template engine that draws inspiration from the excellent design of Django templates, providing a concise and efficient way to handle these logic.Today, let's discuss a very practical feature in templates: how to determine if a number (such as an article ID) can be evenly divided by a specific number.

The 'Division Check' filter in AnQiCMS template:divisibleby

The template system of AnQiCMS provides a very practical filter nameddivisibleby. This filter is specifically used to check if a number can be divided by another number.

Its basic syntax is very intuitive:

{{ 待检查的数字 | divisibleby: 目标数值 }}

This filter will return a boolean value (TrueorFalse). If the 'number to be checked' can be divided by the 'target value', it will returnTrue; otherwise, it returnsFalse.

For example, if we want to know a number10whether it can be2divided evenly, we can write it as:

{{ 10 | divisibleby: 2 }}

The output of this code will beTrue. If we were to10replace11, and the result will beFalse.

Actual application scenarios and code examples

UnderstooddivisiblebyAfter the use of the filter, let's see how it plays a role in the AnQiCMS template.

Scenario one: apply different styles to specific elements in the list

Imagine, in an article list, you want to give a special background color to every third article, or insert a separator at the end of each line.This is a very common and useful technique in front-end design.

at AnQiCMS's{% for %}Within a loop, besides accessing the properties of the current loop item (such asitem.Id/item.Title), we can also useforloop.Counterto get the index of the current loop (starting from1). Useforloop.CounterPerforming division check can help us accurately control the display order of elements on the page.

Here is a code example that shows how to add a special to every third article in an article listspecial-styleClass:

{% archiveList archives with type="list" limit="10" %}
    {% for item in archives %}
        <div class="article-item {% if forloop.Counter | divisibleby: 3 %}special-style{% endif %}">
            <a href="{{ item.Link }}">{{ item.Title }}</a>
            <p>{{ item.Description|truncatechars:50 }}</p>
            <span>文章ID: {{ item.Id }},当前排序: {{ forloop.Counter }}</span>
        </div>
        {# 如果希望在每第三篇文章后插入一个分隔符,可以这样: #}
        {% if forloop.Counter | divisibleby: 3 %}
            <div class="divider">---</div>
        {% endif %}
    {% endfor %}
{% endarchiveList %}

In this code block,forloop.Counter | divisibleby: 3It will judge whether the display number of the current article in the list is3a multiple. If it is, it will givedivan element plusspecial-stylea class, so that you can define its special style through CSS.

Scenario two: Distinguish according to the parity of the article ID.

A common requirement is to differentiate the display style based on the parity of the article or product ID, for example, odd ID articles are displayed to the left, even ID articles are displayed to the right, or have different background colors.This helps create a more dynamic page layout.

In this case, we need to use the article'sIdattribute and determine if it can be2divided evenly.

{% archiveList archives with type="list" limit="10" %}
    {% for item in archives %}
        <div class="article-item {% if item.Id | divisibleby: 2 %}even-id-style{% else %}odd-id-style{% endif %}">
            <h3><a href="{{ item.Link }}">{{ item.Title }}</a></h3>
            <p>文章ID: {{ item.Id }}</p>
            <p>{{ item.Description|truncatechars:50 }}</p>
        </div>
    {% endfor %}
{% endarchiveList %}

here,item.Id | divisibleby: 2It will directly check the article's database ID. IfIdit is even, thendivisibleby: 2ReturnTrueelements gainedeven-id-styleelse gainodd-id-style.

more creative applications

The flexibility of this integer division judgment is not limited to this. You can use it to create more creativity:

  • special content for specific IDs:For example, if a certain categoryIdIs5displays a special category description.
  • Group display:In a complex list of products, eachNproduct is grouped usingdivisiblebyTo determine the start or end of a group, add a group title or style.
  • Content trigger:Determine whether a numerical property (such as product inventory, user points) has reached a certain multiple, triggering specific prompts or discount information.

Notes and tips

  1. Ensure the variable is of numeric type: divisiblebyThe filter expects a number as input. In AnQiCMSId/forloop.CounterDirectly use built-in numeric variables. If it is a custom field, make sure the data type is set to numeric.
  2. withifTag combination: divisiblebyThe filter returns a boolean valueTrueorFalseTherefore, it usually needs to be used with the AnQiCMS template,{% if %}combined with logical judgment tags to bring out its function, achieving conditional content display or style modification.
  3. item.Idvsforloop.Counter:
    • Useitem.Id | divisibleby: NIt is determined by the real and unique numeric identifier recorded in the database.This means that even if the list order changes, the same ID article will still receive the same judgment result.
    • Useforloop.Counter | divisibleby: NIt is based on the current article inthe order of the rendering list on the pageTo judge. This means that the result is related to the pagination, sorting, and other display logic of the page. Which one to choose depends on your specific needs.

Summary

BydivisiblebyFilter, AnQiCMS templates provide website developers and operators with simple and powerful tools, allowing easy implementation of conditional judgments based on digital division.Whether it is to optimize the display of the list, implement odd and even row styles, or build more complex dynamic content layouts, this little filter can help you achieve your goals in an efficient and elegant way.Fully utilize the flexibility of the AnQiCMS template to make your website content more attractive!


Frequently Asked Questions (FAQ)

1.divisiblebyCan the filter only be used for article ID or list number?

No,divisiblebyThe filter can be used for any numeric variable. Other than article ID (item.Id) and loop number (forloop.CounterIn addition, you can also apply it to category IDs, user IDs, or any numeric fields you create in your custom models in the background.If the value of the variable is a number, you can use this filter to make a judgment.

How can I determine if a number cannot be divided by a specific value?

You can combine{% if not %}The syntax to implement the judgment of "not divisible". For example, if you want to find articles that cannot be3divided by:

{% if not item.Id | divisibleby: 3 %}
    {# 此处的代码将在文章 ID 不能被 3 整除时执行 #}
{% endif %}

3.divisiblebyCan it be used for string values? For example, if my article ID is a string like '123'?

TheoreticallydivisiblebyThe filter expects to receive a number. In some cases, the AnQiCMS template engine will automatically try to convert strings that look like numbers to