In the template development of AnQi CMS,replaceThe filter is an important tool for processing string content, which can help us flexibly modify the output text. The basic function of this filter is to find the "old content" in the string (oldThe parameter is) and replace it with “new content”(newThe parameter). However, when we specifically set the parameter to an empty string,newThe parameter is intentionally set to an empty string,replaceThe filter has a very concise and powerful ability -to remove or delete content.

Imagine you have a piece of text read from a database that contains some specific characters, phrases, or even accidental tags that you do not want to display on the frontend. In this case, if you willreplacein the filternewLeave the parameter blank, which will be 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 matcholdParameters define substrings, and they are removed from the original string completely without leaving any replacements.

For example, suppose you have a news headline is"【独家报道】安企CMS助力企业高效管理内容"But you want to remove it when displaying on the home page"【独家报道】"Use this prefix.replaceFilter and thennewSet the parameter to an empty string, and you can implement it like this:

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

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

This seemingly simple function, however, has a wide range of practical application scenarios in the daily work of content operation:

  1. Clean redundant or format markers:Content publishers sometimes add some internal markers, such as[推荐]/(重要)/#标签#and, used for background management or internal identification. These tags are usually redundant when displayed on the front end. Byreplacefiltering them out, we 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, utilizingreplaceFiltering temporarily at the template level is a convenient method.
  3. Uniform content output format: If your content comes from a variety of sources, there may be inconsistent text formats, such as different separators (产品名称 - 型号/产品名称_型号Remove unnecessary characters to unify the front-end display style and enhance user experience.
  4. Adjust content dynamically.: When performing A/B testing, personalized display, or displaying different content based on user identity, it may be necessary to conditionally hide or remove certain fixed phrases, link text, or copyright statements from the page.replaceThe filter provides a flexible template-level control method without touching the original data.

In the Anqi CMS template file,replaceThe syntax for 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 breaks<br>You can use{{ archive.Description|replace:"<br>," }}to remove them, so that the text remains on a single line in a specific layout.

Details and **practice** to note:

  • Match all instances:replaceThe filter will match and remove all occurrences of the stringoldThe content defined by the parameter. If you only want to delete the first match, 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 Anqicms providedstriptagsfor example, a filter (such as{{ 变量|striptags }})striptagsIt is specially designed for safely and completely removing HTML tags, it can better handle complex HTML structures, and avoid layout chaos or content damage caused by simple character replacement.replaceThe filter is more suitable for deleting plain text strings or known structured marks.
  • It will not change the source data: Remember, ``