AutoCMS (AutoCMS) benefits from the efficient and flexible template engine of Go language, providing powerful support for content management.During template development, we often use various filters to format and process data.get_digitFilter is a utility used to extract specific position digits from numbers.So, when we use it to extract a digit from a position in a number that actually does not exist, how will it respond?Let's delve into it.
Deep understandingget_digitFilter
Firstly, let's briefly review.get_digitFilter basic function.This filter was designed initially to conveniently extract the corresponding number from a specified 'reverse position' of a number.The "reverse position" is counted from the right side (units place) of the number, and the counting starts from 1.
For example, if we have a number1234567890:
{{ 1234567890|get_digit:1 }}it will return0(The first from the end){{ 1234567890|get_digit:2 }}it will return9(The second from the end){{ 1234567890|get_digit:10 }}it will return1(The tenth from the end)
This feature is very useful when it is necessary to display numbers in segments or perform specific validations.
When the position does not exist:get_digitUnveiling the behavior
Now, let's return to our core issue: If the position of the number being attempted to retrieve is outside the range of the original number,get_digitwhat will the filter return?
According to the design principles and documentation of AnQiCMS, when you try to get anon-existent number positionwhenget_digitfilter andit will not report an errorInstead, it will adopt a fault-tolerant mechanism, directlyReturn the entire number.
For example:
- If the number is
12345, you try{{ 12345|get_digit:0 }}(Get the last 0th digit, which does not exist) or{{ 12345|get_digit:10 }}(Obtain the last 10th digit, which also exceeds the range of 5 digits), then both of these cases will return12345.
This design takes into account the robustness of template rendering and user experience.In practical applications, we may not always be able to accurately know the length of a number.If an error is thrown when encountering a non-existent position, it may cause the page rendering to break.The original number is returned, allowing developers to make further judgments and processing in the template, or at least ensures the normal display of the page.
Special handling of strings
It is worth noting that,get_digitThe filter is primarily designed to handle numbers, but when it receivesstring typeThe behavior will be different when the value is,.In this case, it will try to locate the characters in the string in the same reverse order, and return the ASCII value of the character minus 48.
For example:
{{ "anqicms"|get_digit:2 }}it will return61This is because the ASCII code of the second-to-last character 'm' is 109, and 109 minus 48 equals 61.{{ "ANQICMS"|get_digit:2 }}it will return29The ASCII code of the character 'M' is 77, 77 minus 48 equals 29.
This behavior is different from the concept of directly extracting numbers, and it usually requires special attention when dealing with non-numeric data to avoid unexpected results.
Practical suggestions and **practice
Understandingget_digitThe fault-tolerant mechanism of the filter helps us write more robust template code. In practical applications, if the length of the input number is uncertain and we want to control the extraction result precisely, we can consider adding length judgment or default value handling in the template logic:
{% set myNumber = 12345 %}
{% set position = 7 %} {# 尝试获取一个不存在的位置 #}
{% set extractedDigit = myNumber|get_digit:position %}
{# 判断是否返回了原始数字,从而得知位置是否存在 #}
{% if extractedDigit == myNumber %}
<p>尝试获取的位置不存在,返回了原始数字: {{ extractedDigit }}</p>
{% else %}
<p>提取到的数字是: {{ extractedDigit }}</p>
{% endif %}
Through this way, you can handle various data scenarios more flexibly, ensuring the stability and accuracy of template rendering.
Common Questions (FAQ)
Q:
get_digitFilters return what when there are no parameters (for example{{ value|get_digit }})?答:According to the AnQiCMS document example, whenget_digitthe filter does not have any parameters, its behavior is similar to trying to access a non-existent position directly,Return the entire numberInstead of trying to extract any digit.Question: If I need to extract the first digit (e.g., the hundred's place, the thousand's place) instead of the last one, how should I operate?Answer:
get_digitThe filter isStart counting from the reverse position of the numberEnglish, and counting starts from 1.If you need to extract numbers from the left (high position), you first need to determine the total length of the numbers, then calculate the inverse position of the high position.1234567890The highest digit (1) is located at the 10th position from the end, so you can use{{ 1234567890|get_digit:10 }}to get.Q:
get_digitDoes the filter handle negative numbers?答:EnglishQiCMS文档中未明确