In AnQiCMS template development, flexible handling and displaying data is the key to enhancing website functionality and user experience.When faced with a series of numbers, such as product numbers, order numbers, or some kind of identification code, sometimes we do not need the entire number, but rather a specific digit at a certain position.get_digitThe filter has become a very practical tool.It can help us accurately extract the required part from numbers, providing more possibilities for the dynamic display of templates and logical judgment.
Core function: understandget_digitFilter
get_digitThe main function of the filter is to obtain a single digit from a number at a specified position. One of its unique features is that the calculation of the position starts from the number'sthe last onestarting, and the index from1start counting.
For example, if there is a number1234567890:
- Get the last one (
get_digit:1),The result is0. - Get the second last one (
get_digit:2),The result is9. - Get the third last one (
get_digit:3),The result is8.
Here is the basic usage:
{{ 源数字 | get_digit:位置 }}
Among them,源数字Is the number you want to process,位置It is an integer representing the position from the end you want to get the number.
It is worth noting that if the specified位置exceeds源数字of the actual number of digits,get_digitThe filter does not report an error, but instead returns the original源数字. This can be used as a simple boundary condition handling mechanism when dealing with numbers of different lengths.
Advanced Usage One: The Subtle Control of Numeric Sequences
get_digitThe filter shows its powerful practicality when it needs to dynamically control logic or style based on specific positions of numbers.
Imagine a scenario, where you would like to display different icons, colors, or layouts based on a certain digit of the product ID. For example, on an e-commerce website, the product ID is12345. You may want to display a red label for products with an odd ID ending and a blue label for even ones.
{% set productId = 12345 %}
{% set lastDigit = productId | get_digit:1 %} {# 获取末位数字,结果是 5 #}
{% if lastDigit % 2 == 0 %}
<span class="product-tag product-tag-blue">热销</span>
{% else %}
<span class="product-tag product-tag-red">新品</span>
{% endif %}
Byget_digit:1ObtainproductIdthe last digit, combined with simple modulo operations.lastDigit % 2 == 0It can easily achieve this kind of dynamic judgment.
Another example is that when displaying a series of contents, you may want to adjust the style at regular intervals. Althoughforloop.CounterIt can meet similar needs, but if your logic is based onIDinstead of the number of loops,get_digitit is very useful. For example, you want to产品IDthe second last digit of0At this time, add a special border to the product list item:
{% for product in productList %}
{% set secondLastDigit = product.Id | get_digit:2 %}
<div class="product-item {% if secondLastDigit == 0 %}highlight-border{% endif %}">
{# 产品详情 #}
<h3>{{ product.Title }}</h3>
<p>ID: {{ product.Id }}</p>
</div>
{% endfor %}
Advanced usage two: hidden treasure of string operations
This isget_digitA filter that is not very intuitive but may be helpful in certain specific scenarios. Although its name suggests it is used for numbers, when源数据Is astringthen,get_digitThe filter will exhibit a special behavior: it will convert the character at a specified position in the string to its corresponding numeric value.
In particular, it will take the character at the position in the string, then get the ASCII code of the character, and then subtract the ASCII code of the number '0' (which is 48).So, if the position is exactly a numeric character ('0' to '9'), you can get the number it represents.If it is not a numeric character, you will get a non-expected number based on its ASCII code.
For example:
{{ "anqicms" | get_digit:2 }} {# 'n' 的ASCII码减去'0'的ASCII码,结果是 61 #}
{{ "ANQICMS" | get_digit:2 }} {# 'N' 的ASCII码减去'0'的ASCII码,结果是 29 #}
{{ "安企CMS" | get_digit:2 }} {# 汉字 '企' 的ASCII码减去'0'的ASCII码,结果是 139 #}
{{ "Product123" | get_digit:3 }} {# 获取倒数第三个字符 '1',结果是 1 #}
Even though this behavior may not be commonly used in regular content operations, as it deals with the ASCII values of characters rather than the literal extraction of numbers, it may come in handy in very few cases where you need to numerize characters at specific positions in a string (for example, some encoding or verification mechanism). But usually, if you need to extract numbers from a string, it is recommended to use string processing functions (such assplitCombineintegerFilter to get more controllable results.
Practical case: Make the template smarter
Dynamic style or class name generationWe can dynamically add CSS classes to HTML elements based on the specific digit positions of a data ID or serial number to achieve visual diversity.
<div class="product-card product-variant-{{ product.id | get_digit:1 }}"> {# ...产品内容... #} </div>Thus, cards with product ID ending in 1 will receive
product-variant-1class, those ending in 2 will receiveproduct-variant-2for fine-grained control of CSS.content grouping or data distributionWhen displaying a large amount of data, if you want to group or filter the data based on a certain feature (such as a number in the ID) initially, you can use it on the front-end.
get_digitImplementation.{% for item in archiveList %} {% set categoryBucket = item.id | get_digit:2 %} {# 获取ID的倒数第二位 #} {% if categoryBucket == 3 %} <p>这是ID倒数第二位是3的特别推荐内容:{{ item.title }}</p> {% endif %} {% endfor %}Simplified data display: Sometimes, only a few digits of a long number (such as a serial number) are needed to identify it in display.
get_digitIt can help you extract the most critical digits.<p>您的订单号尾号是:{{ order.number | get_digit:1 }}{{ order.number | get_digit:2 }}</p>
Points to note
- Counting method: Be sure to remember.
get_digitIs from the number of thethe last oneStart counting and the index from1. - Data type: Although it can be used on strings, its behavior is based on ASCII value conversion. Be cautious when handling data other than numbers, and thoroughly test it.
- Combine with other filters:
get_digitCan be combined with other filters (such asifJudgment,setAssignment) to build more complex template logic.
By deep understandingget_digitThese advanced usage and注意事项 of the filter, you will be able to handle numeric data more flexibly in the AnQiCMS template, bringing users a smarter and more personalized content display experience.
Frequently Asked Questions (FAQ)
Q1:get_digitIf omitted in the filternumberor set it to0What will happen?A1: If you omitnumberParameters (for example{{ 12345 | get_digit }}or set it to0for example{{ 12345 | get_digit:0 }})get_digitThe filter returns the complete original number. This actually means it does not extract any specific