How to use the `get_digit` filter to extract a specific digit from a long numeric ID and display or perform logical operations in an Anqi CMS template?

Calendar 👁️ 72

In the development of Anqi CMS templates, we sometimes encounter situations where we need to extract a certain number of digits from a long numeric ID.For example, you may want to assign different styles to content based on some number of the ID, or make some logical judgments.Now, get_digitThe filter can be put to good use. It provides a concise and efficient way to achieve this goal, making your template logic more flexible.

Understandingget_digitFilter

get_digitA filter is specifically designed to extract digits from a number that are located at specific positions. It counts from the right side (i.e., the units place) starting from the1.

For example, for the number9876543210:

  • Position1Corresponds to the number0(Units).
  • Position2Corresponds to the number1(Tens).
  • Position3Corresponds to the number2(Hundreds).
  • etc.

get_digitusage

get_digitThe basic syntax of the filter is very intuitive:

{{ 您的长数字ID | get_digit:提取位置 }}

Here您的长数字IDusually a variable, such as thearchive.Id.提取位置is a number indicating the number of digits you want to extract (counting from right to left, 1 represents the units place).

Let us understand it through some examples to get a direct understanding of how it works:

Suppose we have a document ID of1234567890:

{# 假设 archive.Id 是 1234567890 #}
<p>原始文档ID: {{ archive.Id }}</p>
{# 提取倒数第一位(个位) #}
<p>个位数: {{ archive.Id|get_digit:1 }}</p> {# 结果为 0 #}
{# 提取倒数第二位(十位) #}
<p>十位数: {{ archive.Id|get_digit:2 }}</p> {# 结果为 9 #}
{# 提取倒数第三位(百位) #}
<p>百位数: {{ archive.Id|get_digit:3 }}</p> {# 结果为 8 #}
{# 提取倒数第十位 #}
<p>倒数第十位: {{ archive.Id|get_digit:10 }}</p> {# 结果为 1 #}

It is worth noting that if提取位置With0or exceeds the total number of digits,get_digitThe filter returns the entire original numeric ID. This may be useful in certain specific scenarios, such as when used as a default value or for error handling.

{# 假设 archive.Id 是 1234567890 #}
<p>位置为0: {{ archive.Id|get_digit:0 }}</p> {# 结果为 1234567890 #}
<p>位置超出总位数: {{ archive.Id|get_digit:15 }}</p> {# 结果为 1234567890 #}

In the actual application of the Anqi CMS template

get_digitThe filter can not only be used to directly display numbers, but also its strength lies in applying it to the logical judgment and dynamic content generation of templates.

1. Content classification and style display

You can use the extracted numbers as a basis for classification, adding unique styles or visual identifiers to different content blocks.

{# 假设 archive.Id 是 987654321 #}
{% set last_digit = archive.Id|get_digit:1 %} {# 结果为 1 #}
<div class="content-item category-{{ last_digit }}">
    <h3>{{ archive.Title }}</h3>
    <p>该内容的ID个位数为: {{ last_digit }}</p>
    {# ... 更多内容 ... #}
</div>

{# 假设您想根据ID的倒数第二位来决定卡片颜色 #}
{% set second_last_digit = archive.Id|get_digit:2 %} {# 结果为 2 #}
{% if second_last_digit == 1 %}
    <div class="card card-red">...</div>
{% elif second_last_digit == 2 %}
    <div class="card card-blue">...</div>
{% else %}
    <div class="card card-default">...</div>
{% endif %}

2. Logical judgment and conditional rendering

CombineifLabel, you can perform different logic based on the extracted number to achieve dynamic display of content.

{# 假设 archive.Id 是 1234567890 #}
{% set last_digit = archive.Id|get_digit:1 %}

{% if last_digit == 0 %}
    <p class="special-note">此文档的ID个位数为0,属于特殊推荐内容。</p>
{% elif last_digit > 5 %}
    <p class="highlight">此文档的ID个位数大于5,请重点关注。</p>
{% else %}
    <p class="normal-item">此文档的ID个位数小于等于5。</p>
{% endif %}

{# 甚至可以结合其他过滤器进行更复杂的判断,例如判断奇偶性 #}
{% set third_last_digit = archive.Id|get_digit:3 %} {# 结果为 8 #}
{% if third_last_digit|divisibleby:2 %}
    <p>这个文档的百位数是偶数。</p>
{% else %}
    <p>这个文档的百位数是奇数。</p>
{% endif %}

In this example, we first useget_digitUsed the third last digit of the ID, then utilizingdivisiblebyThe filter determines whether a number can be divided by 2 to determine its odd or even nature. This combination of using filters greatly enhances the expressiveness of the template.

Summary

get_digitThe filter is a small but powerful tool in the Anqi CMS template.By it, you can easily extract the required number of digits from a long digital ID and use it to create dynamic visual effects, achieve fine content classification, or build complex conditional judgment logic.Mastering this skill will help you manage and present website content more flexibly and efficiently.

Frequently Asked Questions (FAQ)

  1. get_digitCan the filter handle non-numeric strings? get_digitThe filter is mainly used to process numbers. If a non-numeric string is passed, its behavior may not extract the 'number' as expected, but rather try to obtain the internal numerical representation of the character at the corresponding position, which is usually not the result we want.Therefore, it is recommended that you onlyget_digitApply it to variables that are definitely numeric.

  2. If I want to extract digits from the left of a number, what is a good method? get_digitThe filter is specifically designed to extract numbers from right to left (starting from the unit place). If you need to extract from the left, you can consider converting the number to a string and then using other string processing features of the Anqi CMS template, such assliceFiltering is performed.

  3. get_digitCan the extracted numbers be used for mathematical operations?Yes,get_digitThe filter produces numeric results, so you can directly use them in various mathematical operations (such as addition, subtraction, multiplication, division) or compare them with other numbers, and also combine themdivisiblebyFilters related to mathematics can make more advanced logical judgments.

Related articles

In the Anqi CMS template, what is the difference between the `default` and `default_if_none` filters when the variable is `nil` or empty?

During the template creation process of AnQi CMS, flexibly using variables and their default values is the key to improving the robustness and user experience of the template.When you are dealing with a variable that may not exist (`nil`) or is empty, the `default` and `default_if_none` filters are the tools you commonly use.Although they can all provide an alternative output when the variable 'no value' occurs, their criteria for determining 'no value' are subtle and important.To understand the difference between the two

2025-11-08

The `stringformat` filter supports which formatting verbs in Go? How to choose the most suitable output format in AnQi CMS template?

In Anqi CMS template, the precise display of data is a key factor in improving user experience and website professionalism.The `stringformat` filter is exactly such a tool, allowing us to format various types of data in a highly flexible way to meet diverse front-end display requirements.It draws inspiration from the powerful `fmt.Sprintf` syntax in Go language, allowing developers and content operators to accurately control the format of the output content.###

2025-11-08

How to use the `stringformat` filter in Anqi CMS to output Go language struct data in a debug-friendly format (such as `%#v`) to the template?

During the template development process of Anqi CMS, understanding data structures is the foundation of efficient work.Especially when we need to debug complex Go language struct data, if we only use the conventional variable output method `{{ variable_name }}`, we can only see simple values, even memory addresses, which is not very helpful for understanding the overall picture of the data and locating problems.Fortunately, AnQi CMS provides a powerful `stringformat` filter,配合Go语言的 `%#v` formatting verb

2025-11-08

In Anqi CMS template, how does the `stringformat` filter accurately format any number into a string with a specified number of decimal places?

The formatting of numbers in the presentation of web content is often a key detail that reflects professionalism.Whether it is product prices, statistical data, or financial reports, precisely controlling the output of decimal places can not only improve user experience but also ensure the accuracy of information.In Anqi CMS template design, the `stringformat` filter is the powerful assistant to solve this problem.

2025-11-08

The `length` filter calculates the length of Chinese string in an AnQi CMS template, is it counted by bytes or characters? How does it affect the output?

In AnQi CMS template development, dealing with string length is a common requirement.When dealing with multilingual content, especially when it includes Chinese characters, how a string's length is calculated in bytes or characters can directly affect the accuracy of template output and the presentation of the content.The `length` filter used in the Anqi CMS template is designed to solve this problem.It counts the length of the string by characters rather than by bytes.This means, whether it is an English letter

2025-11-08

How to use the `length_is` filter in an Anqie CMS template to validate the length of user input or data lists and return a boolean value for conditional rendering?

In Anqi CMS template development, flexibly controlling the display method of content is the key to improving website user experience.Among them, the `length_is` filter is a very practical tool that can help us easily check the length of user input or data lists in templates and conditionally render based on the check results.The core function of the `length_is` filter is to determine whether the length of a variable (whether it is a string, array, or mapping) is equal to a predefined value.It does not directly return the specific length value

2025-11-08

How to use the `divisibleby` filter in AnQi CMS template to achieve alternate row coloring or group output every N elements?

In website operation, a well-designed page layout and content display can significantly improve user experience and readability.Especially in scenarios with a long list of items, if all entries are presented in the same style, it is easy to cause visual fatigue.At this time, alternating line colors or grouping by a specific quantity can well solve these problems.

2025-11-08

In addition to `stampToDate`, how can the `date` filter of Anqi CMS implement custom date and time formatting when handling `time.Time` type?

In Anqi CMS, the display of dates and times often needs to be adjusted flexibly according to the actual needs of the website.We all know that the `stampToDate` filter is very convenient for handling Unix timestamps, as it can easily convert a string of numeric timestamps into the date format we need.But sometimes, the date value we handle in the template may already be a `time.Time` type object in Go language, rather than the original timestamp.In this case, Anqi CMS provides another powerful tool, that is `date`

2025-11-08