In AnQiCMS template development, we often use various filters to process data, among whichget_digitThe filter is a convenient tool used to extract specific digits from numbers. However, when we turn our attention to a more common but potentially misunderstood scenario-get_digitA filter encounters anon-numeric stringWhen this happens, its behavior becomes less obvious.
First, let's reviewget_digitHow a filter performs when dealing with pure numbers. When the data is of standard numeric type,get_digitThe filter works in a very intuitive way. It can extract the corresponding digit from a number based on the reverse position we specify (starting from the right, position starts from 1). For example:
{{ 1234567890|get_digit:3 }}will return8(Counting from the right, the third digit is 8).{{ 1234567890|get_digit:2 }}will return9(Counting from the right, the second digit is 9).- If the specified digit position exceeds the length of the original number, it will usually return the original number itself, for example
{{ 1234567890|get_digit:15 }}It still returns1234567890.
However, whenget_digitThe filter receives onenon-numeric stringAs input, its behavior is unexpected. Many users might guess that it might return an error or simply return0Or perhaps trying to convert a character in a string to a number (like ASCII value), but this usually does not match the actual result.According to the documentation and examples provided by AnQiCMS, the actual situation is that it returns a number, but this number is not the kind of number we usually extract from strings.Let's look at a few specific examples:
- For lowercase English strings:
{{ "anqicms"|get_digit:2 }}The result is61 - For English uppercase strings:
{{ "ANQICMS"|get_digit:2 }}The result is29 - For Chinese strings:
{{ "安企内容管理系统"|get_digit:2 }}The result is139
These results clearly indicate that,get_digitThe filter does not immediately throw an error or return an expected numeric bit when it receives a non-numeric string.On the contrary, it generates a number that appears not directly related to the original string's digits.This means that at the bottom, the AnQiCMS template engine is trying to pass a non-numeric string toget_digitWhen filtering, it will first perform some form of internal conversion on the string (for example, using its hash value or some internal numerical representation as a number), and then apply this converted valueget_digitThe logic. Therefore, the result returned here61/29/139Is the result after internal conversion and calculation, not the direct numeric representation of a character in the original string.
So, if you expect to extract a specific character (such as the second character) accurately from a string and treat it as a number,get_digitThe filter is not a suitable choice. Its original intention is to handle pure numeric data types. In practice, to avoid unnecessary confusion and errors, it is recommended to useget_digitBefore the filter, make sure your data is of standard numeric type. If you need to extract or process characters from strings, consider using more specialized string processing filters, such assliceUsed to extract a portion of the string or to achieve the purpose through other template logic.
In short,get_digitThe filter is a tool designed for numeric data. When it encounters a non-numeric string, although it will return a number, the source and meaning of this number are usually not consistent with our expectations of extracting numbers directly from the string.Understanding this will help us use AnQiCMS templates more efficiently and accurately.
Frequently Asked Questions (FAQ):
get_digitCan the filter be used to extract numbers from text containing numbers (such as "Order number: 12345")?No.get_digitThe filter is designed to extract frompure numbersExtract numbers from specific positions. If the input is 'Order number: 12345', the system will treat it as a non-numeric string and return a unpredictable value (as described above), rather than '1', '2', etc.To extract numbers from such text, more complex string processing or regular expression methods are required, which usually require custom template functions or backend processing.
2