In the template development of Anqi CMS,replaceThe filter is an important tool for processing string content, which helps us modify text output flexibly. The basic function of this filter is to find the "old content" in the string.oldParameter),and replace it with 'new content'newParameter),However, when we set the parameter explicitly to an empty string,newParameter is intentionally set to an empty string.replaceFilter has a very concise and powerful capability -Achieve the removal or deletion of content.

Imagine that you have a piece of text read from a database that contains some specific characters, phrases you do not want to display on the frontend, or even unintended tags that have mixed in. In this case, if you willreplaceIn the filter.newThe parameter is left blank, which means it is set to an empty string (usually represented in template syntax as not entering any content after the comma). Its function changes from "replacement" to "deletion". It will accurately find all that matcholdThe substring defined by the parameter is removed from the original string completely, leaving no substitute in place.

For example, suppose you have a news headline is"【独家报道】安企CMS助力企业高效管理内容",but you hope to remove it when displaying on the homepage"【独家报道】"this prefix. Usereplacefilter andnewthe parameter set to an empty string, you can achieve it like this:

{{ "【独家报道】安企CMS助力企业高效管理内容"|replace:"【独家报道】," }}

After template rendering, you will get"安企CMS助力企业高效管理内容"Such a result. The original."【独家报道】"Completely deleted, making the title more concise.

This seemingly simple function has a wide and practical application scenario in the daily work of content operation:

  1. Clean redundant or format tagsContent publishers sometimes add some internal tags in the article or product description, such as[推荐]/(重要)/#标签#auto, used for background management or internal identification. These tags are usually redundant when displayed on the front end. Byreplacefiltering them out, you can ensure the purity and professionalism of the content.
  2. Remove sensitive or outdated information (front-end display):Although more thorough sensitive word filtering or information updates should be performed at the backend database level, in certain scenarios that require rapid response, such as temporary sensitive word avoidance or quickly removing related promotional slogans after a promotional event ends, utilizingreplaceFiltering at the template level is a convenient method.
  3. Uniform content output format:If your content sources are diverse, inconsistencies in text format may occur, such as different delimiters (产品名称 - 型号/产品名称_型号)。By removing redundant characters, you can unify the display style of the front-end and enhance the user experience.
  4. Dynamically adjust contentIn situations where A/B testing, personalized display, or displaying different content based on user identity is required, it may be necessary to conditionally hide or remove a fixed phrase, link text, or copyright statement on the page.replaceThe filter provides a flexible template-level control method without touching the original data.

In the template files of the Aanqi CMS,replaceThe syntax of using the filter is{{ 变量 | replace:"旧内容,新内容" }}To achieve the delete effect, just leave the新内容parameter empty.

For example, if you have an article summary variablearchive.Descriptionwhich may contain some unnecessary HTML line break tags<br>you can use{{ archive.Description|replace:"<br>," }}Remove them to keep the text on a single line in a specific layout.

Details and practices to be aware of and **Practice:**

  • Match all instances:replaceThe filter will match and remove all occurrences ofoldThe content defined by the parameter. If you only want to delete the first matching item, you need to combine other logic or a specific filter (if any).
  • Use ordinary replacement to delete HTML tags with caution.:If you need to remove HTML tags, it is strongly recommended to use the SafeCMS providedstriptagsfilters (for example{{ 变量|striptags }}).striptagsIt is specially designed for safely and completely removing HTML tags, which can better handle complex HTML structures and avoid page layout confusion or content damage caused by simple character replacement.replaceThe filter is more suitable for deleting plain text strings or known structured tags.
  • It will not change the source data.Please remember, `