In website operation, we often encounter the need to handle various long string IDs, such as product SKUs, order numbers, user IDs, and so on.These ID strings usually contain multiple segments of information, and we may only need a part of the numbers for displaying on the page, statistical reports, or for some business logic judgments.AnQiCMS (AnQiCMS) powerful template engine provides us with flexible tools, capable of easily meeting such needs.

The string processing tool in Anqi CMS template: Filters

AnQiCMS's template engine is built-in with a rich set of 'filters' (Filters), which are like little tools that can perform various processing on variables in the template, including string truncation, formatting, conversion, etc.To extract the part of the number from a long ID string, we will mainly rely on these filters.

Core strategy one: Accurate extraction, extract the required numbers (sliceFilter)

sliceThe filter is the most direct and effective method for processing long strings. It allows you to extract any part of the string based on the starting and ending positions.

Working principle: sliceThe filter goes throughobj|slice:"start:end"in the form to use.

  • obj: Represents the long ID string variable you want to operate.
  • "start:end": Specifies the range to be截取.startIs the starting index (counting from 0),endIs the ending index (does not include the character at this position).

Useful scenario example:Assuming your order ID string is:ORDER-20231026-VIP12345-001where:

  • ORDER-Is a fixed prefix:
  • 20231026Is a date:
  • VIP12345Is a member code:
  • 001Is the order serial number

1. Extract the order serial number “001”:Serial number "001" is located at the last three digits of the entire string. We can start extracting from a specific index. To extract001, it is inORDER-20231026-VIP12345-001The starting index is25(The position of '0', the end index is)28(Not included, i.e., the end of the string).

{% set order_id = "ORDER-20231026-VIP12345-001" %}
{% set serial_number = order_id|slice:"25:28" %}
<div>订单流水号:{{ serial_number }}</div>

output:订单流水号:001

2. Extract the member code '12345':Member code "VIP12345" is located in the middle of the string. We can first extract the part containing "VIP", then process it. To extract12345, it is inORDER-20231026-VIP12345-001The starting index is19(The position of 'V'), the ending index is25(The next position of '5').

{% set order_id = "ORDER-20231026-VIP12345-001" %}
{% set vip_code_with_prefix = order_id|slice:"19:25" %} {# 截取 "VIP12345" #}
{% set vip_number_part = vip_code_with_prefix|slice:"3:" %} {# 从 "VIP" 后面开始截取 "12345" #}
<div>会员编号:{{ vip_number_part }}</div>

output:会员编号:12345

  • Tip: sliceThe filter is very flexible,startorend Anything can be omitted. For example,obj|slice:"5:" It will start from index 5 to the end of the string;obj|slice:":10"It will take from the beginning to index 9.

Core strategy two: Extracted number: Used for display and logic judgment

Once you pass throughsliceThe filter successfully extracted the required numerical part, which can then be used for display or logical judgment.

1. For display:Simply output the variable in the template.

{% set order_id = "ORDER-20231026-VIP12345-001" %}
{% set serial_number = order_id|slice:"25:28" %}
<p>您的订单号是:{{ order_id }},流水号为:<span style="font-weight: bold;">{{ serial_number }}</span></p>

output:您的订单号是:ORDER-20231026-VIP12345-001,流水号为:001

2. Used for logical judgments:If you need to compare the extracted numbers numerically (for example, to determine size), you first need to useintegerThe filter converts it from a string to an integer. The Anqi CMS template engine supports basic logical judgment tags