In website content display, we often encounter some special requirements, such as the need to apply different styles to specific elements in the list, or to make some distinctions based on the parity of the article ID.AnQiCMS A powerful template engine, inspired by 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 certain number (such as the article ID) can be evenly divided by a specific number.
English translation: The 'integer division judgment' filter in AnQiCMS template:divisibleby
The AnQiCMS template system provides a very practical filter nameddivisibleby。This filter is specifically used to check if a number can be evenly divided by another number.
Its basic syntax is very intuitive:
{{ 待检查的数字 | divisibleby: 目标数值 }}
This filter returns a boolean value (TrueorFalse)。If the 'number to be checked' can be divided evenly by the 'target number', it will returnTrueotherwise, returnFalse.
For example, if we want to know whether the number10can be divided by2divisible, we can write it like this:
{{ 10 | divisibleby: 2 }}
This code's output will beTrue. If we replace10with11, the result will beFalse.
Application scenarios and code examples
Understooddivisiblebythe usage of the filter, let's see how it works in the AnQiCMS template.
Scene one: Apply different styles to specific position elements in the list
Imagine that in a list of articles, you want to give a special background color to every third article, or insert a separator at the end of each row.This is a very common and useful technique in front-end design.
In AnQiCMS,{% for %}In the loop, in addition to accessing the properties of the current loop item (such asitem.Id/item.Title), we can also useforloop.CounterCome get the current loop index (from1Start). Useforloop.Counterto perform integer division judgment, which can help us accurately control the display order of elements on the page.
The following is an example of code that shows how to add a special feature to every third article in the 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,forloop.Counter | divisibleby: 3It will judge whether the current article's display sequence number in the list is3multiplier. If so, it will adddivan element.special-styleclass, so that you can define its special styles through CSS.
Scenario two: differentiate based on the odd or even nature of the article ID
Another 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 directly.Idproperties 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: 2Will directly check the article's database ID.IdIf it is even,divisibleby: 2ReturnsTrue,element obtainedeven-id-style; otherwise, obtainedodd-id-style.
More creative applications
The flexibility of this integer division judgment is not limited to this. You can use it to achieve more creativity:
- Exclusive content for specific IDs:For example, if a category
IdYes5is selected, a special category description will be displayed. - Group display:In a complex product list, each
Nproduct is grouped usingdivisiblebyTo determine the start or end of a group, add a group title or style. - Content trigger:Determine if a certain numeric attribute (such as product inventory, user points) reaches a certain multiple, triggering specific prompts or discount information.
Notes and tips
- Ensure the variable is of numeric type:
divisiblebyThe filter expects a number as input. In AnQiCMSId/forloop.CounterThe built-in numeric variables can be used directly. If it is a custom field, please ensure that its data type is set to numeric. - With
ifLabel combination:divisiblebyThe filter returns a boolean valueTrueorFalseTherefore, it usually needs to be used with AnQiCMS templates,{% if %}combined with logical judgment tags to take effect, achieving conditional content display or style modification. item.Idvsforloop.Counter:- Use
item.Id | divisibleby: NIt is based on the actual, unique numeric identifier recorded in the database.This means that even if the order of the list changes, articles with the same ID will still receive the same judgment result. - Use
forloop.Counter | divisibleby: NWhen, it is based on the current article inthe order in the page rendering listTo judge. This means that the result is related to the pagination, sorting, and other display logic of the page. The choice depends on your specific needs.
- Use
Summary
PassdivisiblebyFilter, AnQiCMS templates provide developers and operators with simple and powerful tools that can easily implement conditional judgments based on integer division.Whether it is to optimize the display of the list, implement styles for odd and even rows, or build more complex dynamic content layouts, this small filter can help you achieve your goals efficiently and elegantly.Fully utilize the flexibility of AnQiCMS templates to make your website content more attractive!
Common Questions (FAQ)
1.divisiblebyFilter can be used for article ID or list sequence number?
No,divisiblebyFilter can be used for any numeric variable. Besides article ID (item.Id) and loop sequence number (forloop.CounterIn addition, you can also apply it to classification ID, user ID, or any numeric field you create in your custom model in the backend.If the value of the variable is a number, this filter can be used for judgment.
2. How can I judge if a certain number 'cannot' be divided evenly by a specific value?
You can combine with{% if not %}The syntax to implement "not divisible" judgment. For example, if you want to find articles that are not divisible by3divisible articles:
{% if not item.Id | divisibleby: 3 %}
{# 此处的代码将在文章 ID 不能被 3 整除时执行 #}
{% endif %}
3.divisiblebyCan it be used for string values? For example, if my article ID is in the form of a string '123'?
TheoreticallydivisiblebyThe filter expects a number. In some cases, the AnQiCMS template engine will automatically try to convert strings that look like numbers.