In the daily content operation of AnQi CMS, we often encounter situations where we need to refine the text displayed on the front end of the website. For example, some article titles, product descriptions, or content paragraphs may contain placeholders (such as【新品】/[限时]/##草稿##These placeholders have specific meanings in the background management, but when displayed to users, we hope to remove them to ensure the cleanliness and professionalism of the content.
The template system of AnQi CMS provides powerful filter functions, which can help us easily achieve this text processing requirement. This article will focus on how to utilizetrimSeries filter, as well as in certain situationsreplaceFilter to accurately remove specific placeholders at the beginning or end of text.
Accurately remove placeholders at both ends of text:trimFilter
trimThe filter is a tool specifically used in the Anqi CMS template to remove specified characters from both ends of a string. When the beginning and end of your text may contain the same placeholder that needs to be removed,trimThe filter performs exceptionally well.
Usage:
{{ 你的变量 | trim:"要移除的占位符" }}
Example:Suppose you have a product title, and to mark it as new, you added placeholders before and after the entry, such as[新品] 安企CMS企业建站系统 [新品]。You want to display only the core information in the front end.
{# 假设 product.Title 的值为 "[新品] 安企CMS企业建站系统 [新品]" #}
{{ product.Title | trim:"[新品]" }}
Result: 安企CMS企业建站系统
Remove only the placeholder at the beginning of the text.trimLeftFilter
In some cases, placeholders may only appear at the beginning of text, or we know for sure that we only need to handle the markers at the beginning.trimLeftThe filter comes into play, it will only remove the specified placeholder from the left (beginning) of the string without affecting the other parts of the string.
Usage:
{{ 你的变量 | trimLeft:"要移除的占位符" }}
Example:If your article title is standardized with【原创】Start with content followed immediately, such as【原创】 安企CMS入门指南. Do you want to display only the article title on the page?
{# 假设 article.Title 的值为 "【原创】 安企CMS入门指南" #}
{{ article.Title | trimLeft:"【原创】 " }}
{# 注意占位符后有一个空格,如果实际文本中有,这里也需要包含 #}
Result: 安企CMS入门指南
###