In AnQiCMS template creation, we often need to control the display of content based on some mathematical logic, such as determining whether one number can be evenly divided by another. At this time,divisiblebyThe filter is particularly important. It helps us easily implement this judgment in the template without writing complex logic code.

divisiblebyThe filter returns a very intuitive and easy-to-understandBooleanThis means that its result can only be two possibilities:True(true) orFalse(False). It does not return the quotient after division nor the remainder; its only purpose is to clearly answer the question of whether one value can be completely divided by another.

Specifically, when you usedivisiblebyA filter to determine if a number (or a value that can be converted to a number) can be completely divided by another number:

  • If it canCompletely divisibleIf it is divisible and the remainder is zero, then the filter will returnTrue.
  • IfIt cannot be divided completelyThat is, there is a remainder, then the filter will returnFalse.

This design enablesdivisiblebyThe filter is very suitable for conditional judgment and controlling the display status of elements in the template.For example, you may want to add different styles to the elements of the list every few lines, or display different prompts based on whether a number is a specific multiple.

UsedivisiblebyThe syntax of the filter is simple: you need to provide a detected value as input, then through a colon:Specify a divisor.

Here are some actual examples that can help you better understand its working principle and return type:

<!-- 示例一:简单整数整除 -->
{{ 21 | divisibleby: 3 }}
<!-- 输出: True (因为 21 可以被 3 完全整除) -->

<!-- 示例二:字符串形式的数字作为除数 -->
{{ 21 | divisibleby: "3" }}
<!-- 输出: True (AnQiCMS 模板引擎会智能地将字符串“3”转换为数字进行计算) -->

<!-- 示例三:浮点数作为被除数 -->
{{ 21 | float | divisibleby: "3" }}
<!-- 输出: True (21 即使被转换为浮点数 21.0,仍然可以被 3 完全整除) -->

<!-- 示例四:不能整除的情况 -->
{{ 22 | divisibleby: "3" }}
<!-- 输出: False (因为 22 不能被 3 完全整除,会留下余数) -->

<!-- 示例五:使用变量作为被除数和除数 -->
{% set my_number = 84 %}
{% set my_divisor = 42 %}
{{ my_number | divisibleby: my_divisor }}
<!-- 输出: True (因为 84 可以被 42 完全整除) -->

<!-- 示例六:在条件语句中使用 -->
{% set current_item_index = 5 %}
{% if current_item_index | divisibleby: 2 %}
    <p>这是偶数行内容</p>
{% else %}
    <p>这是奇数行内容</p>
{% endif %}
<!-- 输出: <p>这是奇数行内容</p> (因为 5 不能被 2 整除,结果为 False) -->

We can clearly see from these examples that whether the input is a direct number, a number in string form, or a floating-point number converteddivisiblebyThe filter always returns aTrueorFalseA boolean value is used to indicate the result of an integer division operation. This makes it a powerful and convenient tool for implementing conditional logic and dynamic content display in AnQiCMS templates.


Frequently Asked Questions (FAQ)

Q1:divisiblebyCan the filter return the remainder or quotient after division?

A1:No.divisiblebyThe design purpose of the filter is very pure, it is only used to determine whether a number can be "completely" divided by another number and return a boolean value (TrueorFalseIt does not calculate or return the quotient of the division, nor does it return the remainder produced by the operation. If you need these values, you may need to combine them with other mathematical operations or template functions to achieve this.

Q2: If the detected value or divisor is not a numeric type,divisiblebyhow will the filter handle it?

A2:The template engine of AnQiCMS is processingdivisiblebyWhen filtering, it will try to intelligently convert the incoming parameters (whether they are the detected values or divisors) into numeric types. If the input is something like"21"Such a numeric string, it is usually able to be correctly converted and participate in calculations. However, if the string passed in is completely unconvertible to a number (such as"hello"), this may cause the operation to fail, it usually returnsFalseTherefore, to ensure stability and expected results, it is recommended to always pass valid numbers or numeric strings.

Q3: BesidesdivisiblebyWhat basic mathematical operations does the AnQiCMS template support?

A3:The AnQiCMS template engine provides a flexible way to perform basic mathematical operations. You can directly use the plus sign in the template expression.+、hyphen-Multiplication sign*And division sign/Perform arithmetic operations, for example{{ item.Price * item.Quantity }}In addition, it providesaddA filter for addition operations, as well astag-calc.mdThe more comprehensive arithmetic operation tags mentioned, supporting floating-point number comparison, modulus, logical expressions, etc., can meet the mathematical calculation needs of most templates.