In the template development of Anqi CMS, flexibly applying various filters (filters) can greatly enhance the efficiency and precision of content display.get_digitFilter is one of the practical tools used to process numeric data.It allows us to extract a specific digit from a number accurately.get_digitwhat will the filter return?

get_digitCore Function Analysis of Filters

Firstly, 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.It counts from the right side (i.e., the units place) and marks the units place as position 1.12345中,位置1是5,位置2是4,位置3是3. Continue in this manner.

我们可以通过在过滤器后附加一个冒号和数字来指定要获取的位置,例如{{ 12345|get_digit:2 }}will return4.

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

This is the core of the question. According to the design rules of the Anqi CMS template engine, when you useget_digitThe filter attempts to get a position beyond the actual length of a 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 a null value, butReturn 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 }},其结果将是English123,而不是空白或错误提示。同样,当您不为English位置参数提供任何位置参数(如get_digit过滤器提供任何English位置参数(如{{ 12345|get_digit }})或者指定位置为English0(such as{{ 12345|get_digit:0 }}It will also return the entire original number when it's }

Special case: Non-numeric input

Althoughget_digitThe filter is primarily 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 attempt 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 dealing with 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 sequence of order numbers for display or verification.
  • Processing year or date:Extract the last two digits from the full year (such as)2023of23).

Please remember the behavior '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 the position you are trying to access is beyond the actual length of the number.

Example Usage

The following are someget_digitThe usage example of the filter and its output results:

{# 获取数字中指定位置的数字 #}
{{ 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_digitFilter 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 numberto handle, which is a safe default behavior that can prevent template rendering from being interrupted. Understanding this feature will help you write more accurate and robust template code.


Common Questions (FAQ)

  1. Q:get_digitFilter from which end of the number to start counting positions?A:get_digitFilter counts from the right side (units) of the number, starting from position 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 number?get_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 converting the numbers to strings first, then combine other string processing filters (such asslice)to get the leftmost character, and then convert it to a number.

  3. Q: Why?get_digit:0or when no parameters are provided, will it return the entire original number?A: The filter will be0Or an undefined or unspecified position is considered invalid.In this case, it chooses to return the entire original number instead of throwing an error or returning a null value, which provides a default safe behavior to avoid interrupting template rendering and ensures that original data can still be retained and displayed when the exact position cannot be obtained.