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