When using AnQiCMS for website content management, we often need to process the displayed data in the template to ensure that the presentation is both beautiful and conforms to business logic.A common requirement is that when certain text content (such as product model numbers, article titles, or image file names) has a specific prefix added, but we want to remove this prefix when displaying on the frontend while retaining the rest of the text.For example, our product model may be unified with a prefix "-", but when displayed externally, we only want to show the model itself.
This question may seem simple, but the key is to distinguish between 'removing the specific characters at the beginning of a string' and 'removing all specific characters in the string'.AnQiCMS is a powerful template engine based on Django template syntax, which provides various filters (filters) to process strings, including a tool specifically designed to solve this kind of problem.
The necessity of accurately removing the prefix of a string
Imagine such a scenario: In order to facilitate internal management and data classification, we have added department identifiers in the background for each product model, such as “R&D Department-Product A”, “Marketing Department-Product B”.But when facing customers on the website front end, displaying "R&D-Product A" is not professional and concise enough, we would rather display it directly as "Product A".
If you simply use the global replacement feature, replacing “R&D-” with empty, then if “R&D-” also appears in the product description, it will be removed as well, which is obviously not the result we want. We need a feature that can identify and remove onlyString beginningA method with a specific prefix.
AnQiCMS's solution:trimLeftFilter
AnQiCMS template engine provides for such needs.trimLeftA filter. This filter is specifically designed to remove the specified characters or strings from the beginning of a string. Its purpose is to accurately handle this 'prefix removal' task.
trimLeftThe principle of operation of the filter:
trimLeftIt will check if the string starts with the keyword you specified (this keyword can be a single character, or a complete string).If it contains, it will remove the prefix part that matches.If the string does not start with the keyword, or the keyword is in the middle of the string,trimLeftno operation will be performed, thereby perfectly avoiding the problem of accidentally deleting non-prefix content.
Usage:
trimLeftThe basic syntax of the filter is very intuitive:
{{ 你的变量 | trimLeft:"要移除的前缀字符串" }}
Let us take the example mentioned at the beginning of the article, assuming that your product model is stored initem.Titlethe variable, and it starts with the prefix "-", we want to remove this prefix:
{# 假设 item.Title 的值为 "前缀-商品名称XYZ" #}
<p>原始商品名称:{{ item.Title }}</p>
<p>处理后商品名称:{{ item.Title | trimLeft:"前缀-" }}</p>
Display result:
原始商品名称:前缀-商品名称XYZ
处理后商品名称:商品名称XYZ
As you can see,trimLeftThe filter accurately removed the prefix "-" from the beginning of the string without affecting the rest.
If you need to remove not a fixed prefix, but a set of variable prefixes (for example, some products may start with "prefixA-", while others with "prefixB-"), you can use it multiple timestrimLeftor, or selectively apply after judgment based on business logic. However, for removing fixed prefixes,trimLeftis undoubtedly the simplest and most efficient solution.
WhytrimLeftis**a choice
In AnQiCMS template, although there are stillcutandreplacestring processing filters, but they have different functional orientations:trimLeftUsed to delete strings in
cutFilter:cutUsed to remove strings inallThe specified characters. If you want to remove all spaces from the string,{{ "Hello World" | cut:" " }}You will getHelloWorldIf used to remove a prefix, it will delete all matching characters in the string, which does not meet our requirement to only remove the prefix.replaceFilter:replaceUsed to replace a specific keyword in a string with another keyword. Although it is theoretically possible to achieve prefix replacement through complex regular expressions or other programming means, in the AnQiCMS template,replaceFilters usually perform global replacements, i.e., replacing all matching keywords. Therefore, it also cannot meet the exact requirement of deleting specific characters only from the beginning of the string.
Compared with that,trimLeftThe filter is designed by AnQiCMS specifically for processing leading characters of strings, it can accurately locate and remove the matched strings at the beginning, which is the most direct and effective way to solve this problem.
Scenarios and more possibilities
trimLeftIts application is not just product models and article titles. In actual website operations, you may use it in the following scenarios:
- Image filename processing:After the image is uploaded, the system automatically adds a timestamp or a user ID as a prefix, such as “20231026-image.jpg”.If the front-end only needs to display "image.jpg", it can be used
trimLeftRemove timestamp prefix. - Optimize URL alias:Some URL paths may have fixed category prefixes, but in certain specific display modules, you may want the URL links to look cleaner, showing only the part without the prefix.
- List data cleaning:When displaying the data list, if some fields in the data source have unnecessary descriptive prefixes (such as "number-","model:",
trimLeftCan help you quickly clean data, improve the readability of the content.
Understand and mastertrimLeftA filter that can make your AnQiCMS template more flexible, efficient, and present refined content that meets user expectations.
Frequently Asked Questions (FAQ)
trim/trimLeftandtrimRightWhat are the differences between filters?trimThe filter is used to remove all leading and trailing spaces or specified characters from a string. For example,{{ " 你好 " | trim }}Will become"你好"If a character is specified, such as{{ "欢迎使用安企CMS" | trim:"欢迎CMS" }}You will get"使用安企", because it will remove all matching characters from both ends of the string.trimLeftThe filter only removes spaces or specified characters from the beginning (left) of the string. For example,{{ " 你好 " | trimLeft }}Will become"你好 "while{{ "前缀-商品" | trimLeft:"前缀-" }}You will get"商品".trimRightThe filter then only removes spaces or specified characters from the end (right) of the string. For example,{{ " 你好 " | trimRight }}Will become" 你好"while{{ "商品-后缀" | trimRight:"-后缀" }}You will get"商品".How do I delete a specific character from the middle of a string?If the goal is to delete a specific character from the middle of a string,
cutorreplaceThe filter is the better choice.cutIt will delete all matching single characters in the string:{{ "He-llo Wor-ld" | cut:"-" }}You will get"Hello World".replaceCan a keyword in a string be replaced with another keyword (including replacing it with an empty string to achieve deletion):{{ "Hello-World" | replace:"-, " }}You will get"HelloWorld".replaceCan a filter be used to remove a prefix?AlthoughreplaceThe filter can be used to replace prefixes with an empty string, for example{{ "前缀-内容" | replace:"前缀-," }}However, this replacement isglobalThis means that if the string also contains the word 'prefix-' in the middle, it will also be replaced. For example,{{ "前缀-内容,这是前缀-说明" | replace:"前缀-," }}You will get"内容,这是说明"Therefore, for removing a specific prefix from the beginning of the string only,trimLeftIs a safer and more accurate choice.