In AnQiCMS template creation, we often need to control the display of content based on some mathematical logic, such as determining whether one number is divisible by another.divisiblebyThe filter is particularly important. It helps us easily implement this judgment in the template without writing complex logic code.
divisiblebyThe filter will return a very intuitive and easy to understand one when performing mathematical integer division judgments.Boolean valueThis means that its result can only be two possibilities:True(true) orFalseIt does not return the quotient after division nor the remainder. Its only purpose is to explicitly answer the question of whether one value can be completely divided by another.
In particular, when you usedivisiblebyFilter to determine whether a number (or a value that can be converted to a number) can be completely divided by another number:
- If it canCompletely divisibleEnglish, that is, it is divisible without a remainder, then the filter will return
True. - Ifis not divisible completelyEnglish, that is, there is a remainder, then the filter will return
False.
This design makesdivisiblebyThe filter is very suitable for conditional judgment and controlling the display status of elements in templates.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 certain number is a specific multiple.
UsedivisiblebyThe syntax of the filter is simple: you need to provide a value to be detected as input, then through the 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) -->
From these examples, we can clearly see that, whether the input is direct numbers, numbers in string form, or converted floating-point numbers,divisiblebyThe filter always returns aTrueorFalseThe boolean value indicates the result of the division operation. This makes it a powerful and convenient tool for implementing conditional logic and dynamic content display in AnQiCMS templates.
Common Questions (FAQ)
Q1:divisiblebyFilter can return the remainder or quotient after division?
A1:Cannot.divisiblebyThe design purpose of the filter is very pure, it is only used to determine whether one number can be "completely" divided by another number and return a boolean value (TrueorFalse)。It does not calculate or return the quotient after division, nor does it return the remainder of the operation. If you need these values, you may need to combine 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:AnQiCMS template engine in processingdivisiblebyThe filter will attempt to intelligently convert the input parameters (whether it is the detected value or the divisor) to a numeric type. If the input is like"21"Such a numeric string, which is usually correctly converted and can participate in calculations. However, if the string passed is completely unconvertible to a number (for example"hello"),then it may cause the operation to fail, it usually returnsFalseOr it may trigger template errors in some strict modes. Therefore, to ensure stability and expected results, it is recommended to always pass a valid number or numeric string.
Q3: BesidesdivisiblebyWhat basic mathematical operations does the AnQiCMS template support?
A3:AnQiCMS template engine provides a flexible way to perform basic mathematical operations. You can directly use the plus sign in template expressions.+、the minus sign-、the multiplication sign*and the division sign/To perform arithmetic operations, for example{{ item.Price * item.Quantity }}In addition, it also providesaddFilter for addition operations, as well as intag-calc.mdMentioned more comprehensive arithmetic operation tags that support floating-point comparison, modulus, logical expressions, etc., which can meet most template mathematical calculation requirements.