In the development of AnQi CMS templates, we sometimes encounter situations where we need to extract a certain number of digits from a long number ID.For example, you may want to assign different styles to content based on a certain number of IDs, or make some logical judgments.get_digitThe filter comes into play. It provides a concise and efficient way to achieve this goal, making your template logic more flexible.
Understandingget_digitFilter
get_digitThe filter is specifically designed to extract a digit from a number at a specified position. It counts from the right side of the number (i.e., the units place), with the position index starting from1Start.
For example, for the number9876543210:
- position
1This corresponds to the number0(Unit place). - position
2This corresponds to the number1(Tens place). - position
3This corresponds to the number2(Hundreds place). - And so on.
get_digitbasic usage
get_digitThe basic syntax of the filter is very intuitive:
{{ 您的长数字ID | get_digit:提取位置 }}
Here are the您的长数字IDIt is usually a variable, such as the article'sarchive.Id.提取位置It 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 its working with some examples:.
Suppose we have a document with ID:1234567890:
{# 假设 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提取位置response for0Or it exceeds the total number of digits,get_digitThe filter will return the entire original numeric ID. This may be very useful in certain specific scenarios, such as default values or error handling.
{# 假设 archive.Id 是 1234567890 #}
<p>位置为0: {{ archive.Id|get_digit:0 }}</p> {# 结果为 1234567890 #}
<p>位置超出总位数: {{ archive.Id|get_digit:15 }}</p> {# 结果为 1234567890 #}
The actual application in the AnQi CMS template
get_digitThe filter can not only be used to directly display numbers, but also has greater power 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 classification basis to add 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_digitExtracted the third last digit of the ID and then useddivisiblebyThe filter determines whether this number is divisible by 2, thereby determining 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.Through it, you can easily extract the required number of digits from a long ID and use it to create dynamic visual effects, implement precise content classification, or build complex conditional judgment logic.Mastering this skill will help you manage and present website content more flexibly and efficiently.
Common Questions (FAQ)
get_digitCan the filter handle non-numeric strings?get_digitFilter is mainly used to process numbers.If the input is a non-numeric string, its behavior may not extract 'number' as expected, but rather try to obtain the internal numeric representation of the character at the corresponding position, which is usually not the result we expect.get_digitApplied to variables that are definitely numeric.What is a good method if I want to extract digits from the left side of a number?
get_digitThe filter is specifically designed to extract digits from right to left (starting from the unit). If you need to extract from the left side, 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.get_digitCan the extracted numbers be used in mathematical operations?Yes,get_digitThe filter extracts results of numeric type, 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 them with other numeric values.divisiblebyPerform more advanced logical judgments related to mathematical filters.