In AnQiCMS template development, realizing dynamic content display and refined control is an everyday challenge for website operation experts.AnQiCMS is highly praised for its concise and efficient template engine, inheriting the elegant style of Django templates, making content display and logical control intuitive.In website operation, we often need to present different contents 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.divisiblebyThe filter can beifThe statement perfectly combines, helping us easily achieve these requirements.
UnderstandingdivisiblebyFilter: Determine 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 if one number can be evenly divided by another number, i.e., with no remainder.divisiblebyit will returnTrue(true); otherwise, returnFalse(false).
Its basic usage is quite concise: you just need to place the number to be judged after the pipe symbol|before, then add after the pipe symboldivisibleby:除数and it is. For example,{{ 21|divisibleby:3 }}it will returnTrue, because 21 can be divided by 3; whereas{{ 22|divisibleby:3 }}Then it will returnFalse, because 22 divided by 3 has a remainder. Even if the number is in string format,"21"【en】A smart type conversion is also performed by AnQiCMS template engine, thus making correct judgments.
todivisiblebyFilter is related toif【en】Combining statements
It is not enough to simply judge whether a number is divisible, we need to execute different template logic based on this judgment result. At this point, we need to introduce the powerful template in AnQiCMS.ifLogic judgment tag.
ifThe statement allows us to decide whether to render a section of template content based on the truth or falsity of a condition. WhendivisiblebyFilter returnsTruewhenifThe code block within the statement will be executed; if the returnFalse, thenifThe code block will be skipped and executed insteadelse(if it exists).
todivisiblebyFilter nested inifthe statement, with a syntax structure 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 several specific examples to seedivisiblebyHow 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 wish to insert a special style or prompt every fixed number of contents.Assume we have a list of articles, and we want to add a special marker to every third article.
<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.Counteris a very useful built-in variable in the AnQiCMS template loop, it represents the current loop count starting from 1. Whenforloop.Counterit can be divided by 3, we add on thedivtaghighlight-itemThe class name is displayed and a special recommendation tag is shown. This way, every third article is highlighted, cleverly breaking the monotony of the list.
Example two: Alternating row styles (zebra strip effect)
An example scenario is setting different background colors for odd and even rows of a table or list to enhance readability, which is commonly referred to as the 'zebra striping' effect. This can also be achieved throughdivisiblebyThe filter can be easily implemented.
`twig
{% archiveList products with type="list" limit="10" moduleId="2" %} {# 假设moduleId="2"是产品模型 #}
{%