AnQiCMS (AnQiCMS) leverages the efficient features and flexible template engine of the Go language to provide powerful support for content management.During template development, we often use various filters to format and process data. Among them,get_digitThe filter is a utility for extracting specific position numbers from numbers.Then, how will it respond when we use it to extract a digit from a number at a position that actually does not exist?Let's delve deeper into it.
Deep understandingget_digitFilter
First, let's briefly review.get_digitThe basic function of the filter. This filter was initially designed to easily extract the corresponding number from a specified "reverse position" of a number.The "reverse position" is counted from the rightmost side (units place) of the number, and the counting starts from 1.
For example, if we have a number1234567890:
{{ 1234567890|get_digit:1 }}will return0(the 1st from the end){{ 1234567890|get_digit:2 }}will return9(the 2nd from the end){{ 1234567890|get_digit:10 }}will return1(the 10th 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_digitbehaviors revealed
Now, let's get back to our core issue: if the position of the number being accessed exceeds the scope of the original number,get_digitwhat will the filter return?
According to the design principles and documentation of AnQiCMS, when you try to access anon-existent number positionthen,get_digitFilter andno error will occur. Instead, it will take a fault-tolerant mechanism, directlyReturn the entire number.
For example:
- If the number is
12345, you try{{ 12345|get_digit:0 }}(Get the 0th last 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 the user experience.In practical applications, we may not always be able to accurately determine the length of a number.If an error is thrown because of a non-existent position, it may cause the page rendering to break.And returning the original number allows developers to make further judgments and processing in the template, or at least ensures the normal display of the page.
Special handling of string
It is worth noting that,get_digitThe filter is mainly designed to handle numbers, but when it receivesstring typeWhen the value is, its behavior will be different. In this case, it will try to locate the character 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 gives us 61.{{ "ANQICMS"|get_digit:2 }}It will return29The ASCII code for character 'M' is 77, 77 minus 48 equals 29.
This behavior is different from the concept of directly extracting numbers, it usually requires special attention when handling non-numeric data to avoid unexpected results.
Practical suggestions and **practice
Understandingget_digitThe error-tolerant mechanism of the filter helps us write more robust template code. In practical applications, if the length of the input numbers is uncertain and you want to control the extraction results accurately, you can consider adding length judgment or default value processing 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 %}
By this means, you can handle various data scenarios more flexibly, ensuring the stability and accuracy of template rendering.
Frequently Asked Questions (FAQ)
Question:
get_digitA filter returns what when no parameters (such as{{ value|get_digit }})?Answer: According to the AnQiCMS document example, whenget_digitthe filter does not take any parameters, its behavior is similar to trying to access a non-existent position, and it will directlyReturn the entire numberNot trying to extract any digit.Question: If I need to extract the first position of a number (such as the hundredth, thousandth), and not the last one, how should I operate?Answer:
get_digitThe filter isCounting from the reverse position of the numberAnd 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 number and then calculate the reverse position of the high position.For example, for a 10-digit number1234567890The highest digit (1) is located at the 10th position from the end, so you can use{{ 1234567890|get_digit:10 }}Get it.Question:
get_digitDoes the filter handle negative numbers?Answer: The AnQiCMS document does not specify clearly