In daily website operations, we often encounter situations where we need to handle various encodings or IDs, such as product codes, order numbers, batch numbers, etc., which often contain multiple segments of information, and we may only need to extract a part of the numbers.For example, from the complex product code "PROD20230815BATCH007", we just want to quickly obtain the batch number "007".
AnQi CMS, with its flexible content model and powerful template tag system, can easily meet such demands.Next, we will discuss how to use the built-in filter function of Anqi CMS to accurately extract the specified digits from a numeric string.
Step 1: Preparation - Define Content Model and Fields
Firstly, we need to ensure that your product codes and other information are stored in the Anqi CMS content model. If you do not have a dedicated field to store these codes, you can set it up through the following steps:
- Log in to the Anqi CMS backend, navigate to the 'Content Management' under 'Content Model'.
- You can choose to modify the existing content model (such as "product model" or "article model") or create a brand new content model.
- On the selected model editing page, find the 'Content Model Custom Fields' section.In here, you need to add a new field, such as named "Product Code", the field type is recommended to choose "Single Line Text".Thus, when publishing or editing product content, you can enter the complete product code in this field.
Assuming we have created a namedproduct_codeThe custom field, used to store product codes.
Step two: Call the product code in the template.
On our website frontend, whether it's the product detail page or the product list page, we first need to obtain the field value that contains the complete encoding. Anqi CMS providesarchiveDetailandarchiveListTags to help us achieve this.
If you are on the product detail page (usually corresponding to)detail.htmltemplate), you can directly go througharchiveDetailtags to get the current product'sproduct_codefield value:
{% archiveDetail productCode with name="product_code" %}
{% set fullProductCode = productCode %}
If you are on the product list page (usually correspondinglist.htmltemplate), you need toarchiveListget each product'sproduct_codefield value:
{% archiveList archives with moduleId="你的产品模型ID" type="page" limit="10" %}
{% for item in archives %}
{% set fullProductCode = item.product_code %}
{# 接下来在这里处理 fullProductCode #}
{% endfor %}
{% endarchiveList %}
Now, we have stored the complete product code infullProductCodeIn the variable, the next step of number extraction can be performed.
The third step: extract the number at the specified position -get_digitThe filter takes action
Anqi CMS is built-in with a very practicalget_digitA filter specifically designed to extract a single digit from a numeric string.This filter starts counting positions from the 'last digit' of the number string, with the position parameter starting from 1.
For example, the product code of our goods isPROD20230815BATCH007we want to extract the last digit of the batch number "7".
The usage is as follows:
{% set productCode = "PROD20230815BATCH007" %}
{% set lastDigit = productCode|get_digit:1 %}
<p>批次号的最后一位是:{{ lastDigit }}</p>
{% set thirdFromLastDigit = productCode|get_digit:3 %}
<p>批次号的倒数第三位是:{{ thirdFromLastDigit }}</p>
After this code runs,lastDigitit will be7,thirdFromLastDigitit will be0.
It should be noted that,get_digitThe filter is mainly used to extract individual numeric characters. If your product code contains non-numeric characters, and you try to extract the position corresponding to a non-numeric character,get_digitIt will try to convert it to the corresponding ASCII value and subtract 48, which may not be the result you expect. Therefore,get_digitIt is most suitable to use when determining the target position is a numeric character.
Step 4: More complex scenarios and general methods:sliceFilter
If we need to extract not a single number, but a continuous sequence of numbers (such as "007"), or product codes mixed with letters and numbers, and we need to extract a specific substring, thensliceThe filter is a more generic choice.
sliceThe filter is used to extract elements from a string or an array at specified positions. It usesfrom:toformat to define the range of extraction, wherefromIs the starting index (counting from 0),toIs the end index (does not include the character at the index position).
Continue with the product codePROD20230815BATCH007For example:
PCorresponding index 0RCorresponding index 1- ...
0(The first digit of the batch number) corresponding index 170(Batch number second digit) corresponds to index 187(Batch number third digit) corresponds to index 19
To extract batch number007It starts at index 17 and ends at index 20 (excluding the character at index 20).
{% set productCode = "PROD20230815BATCH007" %}
{% set batchNumber = productCode|slice:"17:20" %}
<p>提取的完整批次号是:{{ batchNumber }}</p>
After this code runs,batchNumberit will be007.
sliceThe filter is very flexible when handling strings containing mixed characters, as long as you can determine the start and end index positions, you can accurately extract the required substring.
Summary
By following these steps, we see that Anqi CMS provides powerful template tags and filters, making it simple and intuitive to extract specific information from complex numeric strings. Whether it is to extract a single number or a continuous segment of numbers, you can choose according to your specific needsget_digitorsliceFilter. This flexibility greatly improves the efficiency of content operation, allowing your website to display and manage product data more intelligently.
Frequently Asked Questions (FAQ)
1. If my product code contains both letters and numbers,get_digitCan the filter accurately extract the numbers I want?
get_digitThe filter may behave less intuitively when encountering non-numeric characters, it will try to convert the characters to ASCII values for processing. Therefore, if your product code contains letters and numbers and you need to extract the numeric positions that may correspond to non-numeric characters, or if you need to extract multiple digits, it is recommended to use a more generalslicefilter.sliceThe filter will directly cut the substring according to the character index range you specify, whether it is a number or a letter, it can be accurately obtained.
2. I want to extract the batch number not at a fixed position, but by some delimiter (such as a hyphen “-”) identification, does AnQi CMS have a way to do this?
Of course! If the batch number is defined by a specific delimiter, you can usesplitThe filter splits the entire string into an array and then retrieves based on the batch number at the position in the array. For example, the product code isPROD-2023-007, Batch number "007" is the third part, you can do this:
{% set productCodeWithDelimiter = "PROD-2023-007" %}
{% set parts = productCodeWithDelimiter|split:"-" %} {# 拆分成 ["PROD", "2023", "007"] #}
{% set batchNumber = parts[2] %} {# 获取第三部分,索引为2 #}
<p>通过分隔符提取的批次号是:{{ batchNumber }}</p>
3. Extracted numbers (such as inventory quantities) need to perform addition, subtraction, multiplication, and division, can they be operated directly in the template??
Yes, AnQi CMS supports basic arithmetic operations in templates. You can useaddfilters for addition, or directly apply+/-/*//operators in expressions. For example:
`twig {% set stock = “100”|integer %} {# Convert string to integer #} {% set newStock = stock|add:50 %} {# 100 + 50 = 150 #}
The stock quantity after increase is: {{ newStock }}
{% set price = “199.99”|