In website content display, we often encounter situations where we need to adjust the content layout or display logic based on the characteristics of some numbers.For example, we may need to insert an advertisement every few articles, or make the even and odd rows of the table display different background colors, or perform special operations when the list loops to a specific position.In the AnQiCMS template system, based on the Django template engine syntax, it provides a very practical filter that can easily meet this requirement.
This filter isdivisibleby. Its purpose is to determine whether a number (or a value that can be converted to a number) can be evenly divided by another number and returns a boolean value (TrueorFalse)。This is a very direct and efficient solution for scenarios where modular arithmetic judgment is needed in templates.You can find information about in the AnQiCMS template filter documentdivisiblebyDetailed description.
How to usedivisiblebyFilter
The usage is very intuitive, usually{{ 待判断的数字 | divisibleby: 除数 }}. The result of this expression will beTrueorFalse. Therefore, it is often used with conditional judgment tags{% if ... %}to execute different template logic based on the judgment result.
Let us understand its practical application through a few specific examples:
1. Simple integer division judgment
Suppose we want to directly determine whether a fixed number can be evenly divided by another number.
{# 判断 21 是否能被 3 整除 #}
{% if 21|divisibleby:3 %}
<p>21 可以被 3 整除。</p>
{% else %}
<p>21 不能被 3 整除。</p>
{% endif %}
{# 判断 22 是否能被 3 整除 #}
{% if 22|divisibleby:3 %}
<p>22 可以被 3 整除。</p>
{% else %}
<p>22 不能被 3 整除。</p>
{% endif %}
Running the above code will output “21 can be divided by 3.” and “22 cannot be divided by 3.”, indicating that the filter accurately executed the judgment.
2. In the loop, implement interval display or style switching
In the list loop,divisiblebythe filter meetsforloop.Counter(The current loop index, starting from 1) orforloop.Counter0(The current loop index, starting from 0) combined can achieve very flexible layout control.For example, we can make the background color of every two articles different, or insert an ad slot every three articles.
{% for item in archives %} {# 假设 archives 是通过 archiveList 标签获取的文章列表 #}
<div class="article-item {% if forloop.Counter|divisibleby:2 %}even-row{% else %}odd-row{% endif %}">
<h3>{{ item.Title }}</h3>
<p>{{ item.Description }}</p>
{# 每隔 3 篇文章显示一个广告位 #}
{% if forloop.Counter|divisibleby:3 %}
<p class="ad-placeholder">这是每隔3篇文章出现的广告位。</p>
{% endif %}
</div>
{% endfor %}
In this example,forloop.Counter|divisibleby:2It will return when the loop index is evenTruethereby applyingeven-rowStyle, otherwise applyodd-rowStyle. Also,forloop.Counter|divisibleby:3It controls the display frequency of the ad space.
3. Determine the divisibility of dynamic values.
divisiblebyThe filter can not only handle fixed values but also apply well to variables defined in the template.
{% set total_items = 10 %} {# 假设总共有 10 个项目 #}
{% set items_per_page = 3 %} {# 假设每页显示 3 个项目 #}
{% if total_items|divisibleby:items_per_page %}
<p>总项目数 {{ total_items }} 可以被每页显示数量 {{ items_per_page }} 整除,页面正好分完。</p>
{% else %}
<p>总项目数 {{ total_items }} 不能被每页显示数量 {{ items_per_page }} 整除,会有余数。</p>
{% endif %}
This example shows how to use variables asdivisiblebyThe input of the filter, which is used to handle pagination logic or statistics