In the template world of AnQi CMS, efficiently and flexibly handling data is the key to website operators enhancing the expression of content.As an experienced website operations expert, I know that the strength of template language lies in its ability to convert complex technical logic into simple and practical content presentation.Today, we will focus on a seemingly simple but extremely practical requirement: how to accurately extract the specific digit you want from a number in the security CMS template.

The template system of Anqi CMS adopts syntax similar to Django template engine, which enables it to have rich built-in filters (filters) and tags (tags) in data processing. Particularly for numerical operations, such as extracting digits.get_digitFilter is our powerful assistant.

Deep understandingget_digitFilter

get_digitFilter designed specifically to extract specific positions of numbers from numbers. Its usage is very intuitive, usually expressed as{{ 变量 | get_digit:位置 }}。However, to truly master it, we need to understand its unique counting rules:

Core rules: get_digitThe filter extracts numbers from theOn the right sideand starts counting, andThe first position is 1.

This means, if you have a number12345and you want to extract4:

  • 5the one at position 1
  • 4the one at position 2
  • 3Located at position 3
  • 2Located at position 4
  • 1Located at position 5

Therefore, to extract4,you need to use{{ 12345 | get_digit:2 }}.

get_digitThe details of the filter's parameters and behavior

get_digitFilter takes two main parts: **the number (obj), and the specified position (position).

  1. obj(**the number):This can be any number variable in the template, such as document IDarchive.IdProduct Priceproduct.PriceOr it could be a number directly written into the template. For example,{{ 12345 | get_digit:2 }}of12345Which is the number.

  2. position(Specified Position):This parameter is the position of the digit you want to extract. As mentioned earlier, it is an integer starting from the right side of the number and counting from 1.

    • Handling out of range:If the position you specified exceeds the actual number of digits (for example, from123extract the 4th digit),get_digitthe filter will not report an error, but will intelligently returnthe entire original numberThis is a very practical error-tolerant mechanism that can avoid template rendering interruption.
    • Behavior for non-numeric input:Althoughget_digit主要用于数字,但当它被应用于单个字符的字符串时,其行为会涉及到字符的 ASCII 值。例如,对于单字符数字字符串(如{{ "5"|get_digit:1 }}It will return the number directly5; For non-numeric characters (such as{{ "a"|get_digit:1 }}It returns the ASCII value of the character minus 48. In most content operation scenarios, we usually apply it to actual numbers to avoid unexpected results.

Actual application scenarios: Common needs for digital extraction

In the content operation of AnQi CMS, accurately extracting digits can help us achieve various flexible content display and data processing, such as:

  • Parsing of SKU or product code:Some product SKUs may use a fixed format, such asYYMMDDXX(last two digits of the year, month, date, batch number). Throughget_digitFilter, you can easily extract the specific numbers of the production month, date, or batch number, which can be used for front-end filtering, display, or further logical judgment.
  • The format of the order number or user ID:Sometimes, to protect privacy or simplify display, we may need to extract some digits of the order number or user ID for display.
  • The decomposition of date numbers:Although the Anqi CMS providesstampToDatesuch powerful date formatting tools, but in some specific scenarios, if the date is stored in pure numeric format (for example231005代表23年10月05日),您仍然可以使用get_digit来获取年份、月份或日期中的个位数或十位数。

Practice in the Anqi CMS template.get_digit

Now, let's look at a specific example to see how to use it in the security CMS templateget_digitFilter.

Suppose our article (or product) objectarchiveThere is a numeric typeIdField, we hope to display a special icon or category based onIdthe second last digit.

The last digit of the article ID {{ archive.Id }} is: {{ lastDigit }}

{# Get the third last digit of the ID #} {% set thirdLastDigit = archive.Id | get_digit:3 %}

文章ID {{ archive.Id }} 的倒数第三位数字是:{{ thirdLastDigit }} English

{# An example of a more complex operation: Extract the month from the product number (assuming the product number format is YYMMDD, such as 231005) #} {% set productId = 231005 %}

Product Number: {{ productId }}

{% set monthUnit = productId | get_digit:3 %} {# The unit digit of the month #} {% set monthTens = productId | get_digit:4 %} {# The tens digit of the month #}