During the template development process of AnQiCMS, we often use various filters to process and format data.get_digitFilter is one of them, its main function is to extract the specified position from a number. However, when we pass a Chinese string as input toget_digitWhen the filter is applied, its performance may be different from what we intuitively expect.

get_digitThe basic function of the filter

First, let's reviewget_digitThe basic function of the filter. As the name implies, it is designed to obtain specific 'digits' from numbers.When in use, we will specify a position (counting from right to left, starting from 1), and the filter will return the number at that position.If the specified position does not exist, it usually returns the entire original number.For example, for1234567890The number:

  • {{ 1234567890|get_digit:2 }}will return9The second digit from the right is 9.
  • {{ 1234567890|get_digit:10 }}will return1The tenth digit from the right is 1.
  • If the specified number of digits exceeds the length of the number, for example{{ 1234567890|get_digit:15 }}it will return the entire1234567890.

From these examples, it can be seen thatget_digitThe filter is for numbers, it expects the input to be a number that can be split by bits.

Performance under Chinese string input

Then, when we pass the Chinese string toget_digitWhen filtering, how will it handle? From the actual test results, its behavior is not simply to 'extract' a certain 'number' from a Chinese string, but rather to attempt some numerical conversion of characters at specific positions in the string.

For example, if we will安企内容管理系统This Chinese string is used as input, and try to get the 'number' of the second-to-last character:{{ "安企内容管理系统"|get_digit:2 }}We will get139such a result.

Such output results are somewhat unexpected to us, as it is neither a certain Arabic numeral extracted from a string, nor a direct numerical representation of the Chinese character itself.This is because during internal processing, the filter may convert characters to their underlying numerical encoding (such as Unicode code points or UTF-8 byte values) for operations.Because Chinese characters are usually multi-byte encoded, the complexity of their internal representation leads to the final numerical result being quite different from the 'number' concept we usually understand.

Let's see other English examples, such as{{ "anqicms"|get_digit:2 }}May return61while{{ "ANQICMS"|get_digit:2 }}It may return29These numbers are not the ASCII code of letters minus the ASCII code of '0', but have undergone some specific numerical conversion logic.

Key points and usage suggestions

Several important conclusions can be drawn from these examples:

  1. Design intention:get_digitThe filter is mainly designed to handlepure numbersorStrings that can be interpreted as numbersTo extract its digit positions.
  2. Unexpected inputWhen the input is non-numeric characters (especially Chinese characters or ordinary English strings),get_digitThe filter attempts to convert its internal representation to a numeric value and process it. This conversion result is usuallynumericalized, but not in the sense of our usual "number extraction".
  3. the result is unpredictable: For Chinese character strings, due to their multi-byte encoding characteristics,get_digitthe output of the filter will be more abstract and unpredictable, usually not producing numbers with business significance.

Therefore, when usingget_digitWhen filtering, it is recommended that you always ensure that the input is of the expected numeric type, or at least is a string that can clearly represent a number (such as"123")。If your need is to extract numbers from strings containing Chinese characters, or to perform other complex string processing, thenget_digitThe filter may not be the most appropriate tool; you may need to consider other string processing methods or custom logic to achieve this.Correctly understanding and using the functional boundaries of each filter can help us build website functions more efficiently and stably.


Frequently Asked Questions (FAQ)

1.get_digitCan the filter recognize Arabic numerals in Chinese strings? For example, extract '123456' from 'Order number 123456'?No.get_digitThe filter is based on the characters in the stringPositionPerforming processing, and it expects a "number" at this position.It cannot intelligently identify and extract a continuous sequence of Arabic numerals from a mixed string.To implement the extraction of numbers from a mixed string, it usually requires combining other string search or regular expression-related filters (if provided by AnQiCMS) or data preprocessing on the backend.

2. Whyget_digitWhen the filter processes Chinese character strings, it returns values that look irregular instead of error messages?The template engine of AnQiCMS is designed to return a result instead of throwing an error directly when encountering type mismatches that can be handled implicitly or through default logic, in order to maintain the smoothness of template rendering. Forget_digitWhen input is non-numeric characters, it may treat the internal encoding (such as Unicode value or UTF-8 byte data) as a number and extract some numerical part from it.These values are calculated based on the underlying encoding of characters, and therefore, in the absence of context, they may appear to be irregular.

3. If I need to check whether a variable is a pure number for safe useget_digitHow should the filter be done?You can use in AnQiCMS template,ifLogical judgment is used in conjunction with other filters to preliminarily check the variable type.For example, you can try to convert it to a numeric type (if supported) or check if it only contains numeric characters.But the safest way is to perform data validation and type conversion in the backend (Go language) code before passing the data to the template, ensuring that the data passed to the template is already in compliance withget_digitThe expected type is a number.