How to accurately extract a specified digit from a number in Anqi CMS template?

Calendar 👁️ 54

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 1
  • 4at position 2
  • 3at position 3
  • 2At location 4
  • 1At 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)

  1. 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**.

  2. 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, from123extracting 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:Althoughget_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.

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 asYYMMDDXX(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 providesstampToDatesuch 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 #}

Related articles

What is the actual difference between the `default` and `default_if_none` filters when handling variable default values?

In the AnQi CMS template world, we often encounter situations where variables may not have values.For example, the nickname of the user may be empty, the introduction of the article may not be filled in, or a certain configuration item may not exist at all.At this time, in order to make the page display more friendly and information more complete, we need to set up a 'backup' for these variables - that is, the default value.

2025-11-06

How to set a reliable default value for empty variables in Anqi CMS template?

As an experienced website operations expert, I know that handling null variables is the key to ensuring the quality of website content and user experience when building and maintaining website templates.In AnQiCMS such a flexible and efficient content management system, the flexible use of template variables is its strong point, but at the same time, how to elegantly set reliable default values for possibly empty variables to avoid ugly blank spaces on the page or functional anomalies is an art worth in-depth exploration.

2025-11-06

What are some advanced usage techniques for the AnQi CMS filter chaining call?

As an experienced website operation expert, I fully understand the importance of content presentation and processing efficiency in daily work for website operation.AnQiCMS (AnQiCMS) with its flexible and efficient characteristics, has provided us with many conveniences in content management.Among them, the filtering function provided by the template engine, especially the **chained filter call**, is a powerful tool for us to achieve fine-grained content presentation and efficient template development.Today, let's delve into some advanced usage techniques of the Anqi CMS filter chaining call

2025-11-06

How to efficiently apply various data filters in Anqi CMS templates?

AnQi CMS, as an enterprise-level content management system built based on the Go language, has won the favor of many small and medium-sized enterprises and content operators with its efficient, secure, and flexible features.In daily content management and front-end display, we often need to process and present data in a fine-grained manner, which cannot be separated from the powerful data filtering function in the template.This article will delve into how to efficiently apply various data filters in the AnQiCMS template, making your content display more expressive and practical.###

2025-11-06

What does the `get_digit` filter return when it encounters a non-existent position?

AnQiCMS (AnQiCMS) leverages the efficient characteristics and flexible template engine of the Go language to provide strong support for content management.During template development, we often use various filters to format and process data.Among them, the `get_digit` filter is a utility used to extract a specific digit from a number.Then, how will it respond when we use it to extract a digit from a number at a position that actually does not exist?Let's delve deeper into it.

2025-11-06

How to get the accurate length of strings, arrays, or key-value pairs in AnQi CMS templates?

In website content management, we often need to finely control the displayed data, such as limiting the number of characters in article titles, checking if the image collection is empty, or counting how many entries are in a list.As an experienced website operations expert, I know how AnQiCMS' powerful and flexible template engine can help us meet these needs.

2025-11-06

How to use the `{% for ... in ... %}` tag in AnQiCMS templates to iterate over a data list?

How to flexibly use `{% for ...` in Anqi CMS templatein ...How to iterate over a data list with `%}` tag?As an experienced website operations expert, I fully understand the importance of an efficient and flexible template system for content management.AnQiCMS, as an enterprise-level content management system based on the Go language, draws on the essence of the Django template engine in template design, especially its powerful data traversal capabilities, particularly `{% for ...in ...The `tag is one of the core tools for building dynamic page content.

2025-11-06

How to get the index starting from 1 in the current iteration of the `for` loop?

## How to skillfully use AnQiCMS template to easily get the "first" and "Nth" indices in the loop In website content operation, we often need to display a series of contents on the page, such as article lists, product displays, navigation menus, etc.In order to better present the content, we often need to fine-tune the control of each item in the list, such as adding special styles to the first item, or displaying the item's serial number in the list.

2025-11-06