In AnQi CMS template development, flexibly using various filters (filters) can greatly enhance the efficiency and precision of content display.get_digitThe filter is one of the practical tools used to process numeric data.It allows us to extract a specific digit from a number accurately.However, a common issue during use is that if you try to access a position beyond the length of the number itself,get_digitwhat will the filter return?

get_digitFilter's core function analysis

First, let's understandget_digitThe basic function of the filter. This filter is mainly used to get the Nth digit from the end of a number.The counting method starts from the right side (i.e., the units place) and the units place is counted as position 1.For example, in a number12345In, position 1 is5Location 2 is4Location 3 is3and so on.

We can specify the position to retrieve by appending a colon and a number to the filter, for example{{ 12345|get_digit:2 }}it will return4.

What will it return if the specified position does not exist?

This is the core of the problem. According to the design rules of the Anqi CMS template engine, when you useget_digitThe filter attempts to access a position beyond the actual length of the number, for example, if you want to get the third digit from the end of a two-digit number, it will not throw an error or return an empty value, but instead it willReturn the complete original number.

This means that if the system cannot find the corresponding number at the specified location, it will choose to output the original, complete number as the result. For example, for the number123If you try to get{{ 123|get_digit:5 }}The result will be123Not a blank or error message. Similarly, when you do not provide any position arguments toget_digitthe filter (such as{{ 12345|get_digit }}) or specify the position as0(such as{{ 12345|get_digit:0 }}It will also return the entire original number when it is an expression.

Special case: Non-numeric input

Althoughget_digitThe filter is mainly designed for numbers, but if you try to apply it to a non-numeric string, and the specified position of the string cannot be directly parsed as a number, it will try to perform an internal ASCII value conversion. However, this usage is usually notget_digitThe main application scenarios, and the returned results may not be intuitive, therefore it is recommended to ensure that the input is numeric data when using it.

Practical scenarios and precautions

get_digitThe filter is very useful when it needs to process specific numeric logic, for example:

  • Extract specific digits of the order number:In some business scenarios, it is necessary to extract the last few digits from a string of order numbers for display or verification.
  • Processing year or date:Extract the last two digits from the full year (such as2023of23)

Remember the behavior that 'if the position does not exist, return the original number' when using this filter.This can be used as an implicit judgment mechanism: If the result you obtain is the same as the original number, it may mean that you are trying to access a position beyond the actual length of the number.

Usage Example

The following are someget_digitThe use of filter example and its output result:

{# 获取数字中指定位置的数字 #}
{{ 1234567890|get_digit:3 }} {# 结果:8 (倒数第三位是8) #}
{{ 1234567890|get_digit:1 }} {# 结果:0 (倒数第一位是0) #}
{{ 1234567890|get_digit:15 }} {# 结果:1234567890 (数字只有10位,位置15不存在,返回原数字) #}
{{ 123|get_digit:5 }} {# 结果:123 (数字只有3位,位置5不存在,返回原数字) #}
{{ 12345|get_digit:0 }} {# 结果:12345 (参数为0,返回原数字) #}
{{ 12345|get_digit }} {# 结果:12345 (无参数,返回原数字) #}

{# 针对非数字字符串的特殊处理,一般不推荐 #}
{{ "anqicms"|get_digit:2 }} {# 结果:61 #}
{{ "ANQICMS"|get_digit:2 }} {# 结果:29 #}
{{ "安企内容管理系统"|get_digit:2 }} {# 结果:139 #}

Summary:

get_digitThe filter is a convenient tool for handling digit numbers in the Anqi CMS template. When you try to access a non-existent position, it will returnthe entire original numberThis is a safe default behavior to handle, which can avoid template rendering interruption. Understanding this feature helps you write more accurate and robust template code.


Frequently Asked Questions (FAQ)

  1. Q:get_digitThe filter counts from which end of the number?A:get_digitThe filter counts from the right side (units place) starting from 1. For example, in a three-digit number, the units place is position 1, the tens place is position 2, and the hundreds place is position 3.

  2. Q: How do I use to get the first digit (leftmost) of a numberget_digit?A:get_digitThe filter is specifically designed to count from right to left. If you need to get the leftmost number,get_digitThe filter may not be the most direct tool. You can consider first converting numbers to strings, then combining with other string processing filters such assliceTo get the leftmost character, then convert it to a number.

  3. Q: Why?get_digit:0Or if no parameters are provided, it will return the entire original number?A: The filter will be0Or the parameter not provided is considered an invalid or unspecified position.In this case, it chooses to return the entire original number instead of throwing an error or returning an empty value, which provides a default safe behavior, avoiding the interruption of template rendering, and ensuring that the original data is retained and displayed when it is not possible to accurately obtain the position.