In the Anqi CMS template world, efficiently and flexibly handling data is the key for website operators to enhance content presentation.As an experienced website operations expert, I know that the power of template language lies in its ability to transform complex technical logic into simple and practical content presentation.Today, we will focus on a seemingly simple but extremely practical need: how to accurately extract the digit you want from a number in Anqi CMS template.
The AnQi CMS template system adopts syntax similar to the Django template engine, which makes it rich in built-in filters (filters) and tags (tags) for data processing. Particularly for numerical operations, such as extracting digits of numbers.get_digitThe filter is our powerful assistant.
Deep understandingget_digitFilter
get_digitA filter specifically designed to extract specific digits 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 rule: get_digitThe filter extracts numbers from theRightand starts counting fromThe first position is 1.
This means, if you have a number12345and you want to extract4:
5at position 14at position 23at position 32At location 41At location 5
Therefore, to extract4you need to use{{ 12345 | get_digit:2 }}.
get_digitThe parameters and behavior details of the filter
get_digitThe filter accepts two main parts: **the number of actions (obj) and specified position (position)
obj(Made number):This can be any numeric variable in the template, such as document IDarchive.Id, Product priceproduct.PriceOr it could be a number directly written into the template. For example,{{ 12345 | get_digit:2 }}of12345It is a number made by**.position(Specify position):This parameter is the position of the digit you want to extract. As mentioned before, it is counted from the right side of the number, starting from 1 as an integer.- Handling out of range:If the position you specify exceeds the actual number of digits (for example, from
123extracting 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 prevent template rendering from being interrupted. - Behavior for non-numeric input:Although
get_digit主要用于数字,但当它被应用于单个字符的字符串时,其行为会涉及到字符的 ASCII 值。例如,对于单字符数字字符串(如{{ "5"|get_digit:1 }}It will return the number directly5; While 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.
- Handling out of range:If the position you specify exceeds the actual number of digits (for example, from
Common requirement for number extraction in practical applications
In Anqi CMS content operations, accurately extracting numbers can help us achieve a variety of flexible content display and data processing, for example:
- Parsing of SKU or product code:Some product SKUs may use a fixed format, such as
YYMMDDXX(the last two digits of the year, month, date, and batch number). Throughget_digitFilter, you can easily extract the specific numbers of the production month, date, or batch number, for use in front-end filtering, display, or further logical judgment. - Order number or user ID formatting:Sometimes, to protect privacy or simplify display, we may need to extract some digits of the order number or user ID for display.
- Date number decomposition:Although AnQi CMS provides
stampToDatesuch powerful date formatting tools, but in certain specific scenarios, if the date is stored in pure numeric format (for example231005Representing October 05, 2023), you can still useget_digitto obtain the units or tens digits of the year, month, or date.
Practice in the Anqi CMS template.get_digit
Now, let's look at a specific example of how to use it in the Anqi CMS templateget_digitfilter.
Suppose our article (or product) objectarchivehas a numeric typeIdField, we hope to display a special icon or category according toIdthe second last digit.
`twig {# Assume we have an article ID, for example, archive.Id = 1234567 #} {# Get the last digit of the ID #} {% set lastDigit = archive.Id | get_digit:1 %}
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 %}
The third last digit of the article ID {{ archive.Id }} is: {{ thirdLastDigit }}
{# A more complex example: 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 #}