In the content operation practice of AnQiCMS, we often need to do a refined processing of the content output from the template to ensure that the information presented to the user is clean, standardized and meets expectations.This is a common requirement to remove specific characters or symbols from the content.replaceThe filter is one of the simple yet very practical tools.

UnderstandingreplaceThe core function of the filter

replaceThe filter, as its name suggests, is mainly used to search and replace strings in templates.Its basic logic is very intuitive: find all parts that match the "old characterThis is extremely effective for cleaning up content obtained from the background, or for standardizing displayed text.

The basic usage syntax is like this:{{ 变量 | replace:"旧字符,新字符" }}

Here,变量It is the text content you want to operate on,旧字符This is the specific character or phrase you want to search and replace新字符This is what you want to use for replacement旧字符Separate the old character and new character with a comma,Split the content.

For example, if you retrieve a title from the database and want to replace specific abbreviations with full names, you can do it like this:

{# 假设 item.Title 的值为 "欢迎使用安企CMS" #}
{{ item.Title | replace:"安企,AnQi" }}
{# 输出结果:欢迎使用AnQiCMS #}

By this simple template code, we can easily replace '安企' in the title with 'AnQi', making the brand name more standardized.

Use flexiblyreplaceFilter removes content

replaceOne of the most direct applications of the filter is to "remove" unnecessary characters or symbols from text. To achieve this, the method is to set新字符the parameter to an empty string.

1. Remove specific characters or phrases

When you want to completely remove a specific character, word, or phrase from a string, just新字符Leave the parameter blank if needed. This is very useful when cleaning up marketing tags in article titles, content in parentheses of product names, or removing redundant prompts.

The title of a document is “Product Name (New!)”, but when displaying on the front end, you do not want to see the part “(New!)”:

{# 假设 item.Title 的值为 "产品名称 (新!)" #}
{{ item.Title | replace:" (新!)," }}
{# 输出结果:产品名称 #}

Please note,replaceThe filter performs literal replacement and does not recognize regular expressions. Therefore, if the characters you want to remove are(/)such special symbols, make sure you provide旧字符These are literal combinations of symbols, not an attempt to match with regular expressions.

2. Clean up extra whitespace characters.

Content often contains another common issue, which is excessive whitespace, such as irregular spaces between paragraphs of articles, or unnecessary newline characters generated when importing content. Although AnQiCMS providestrimprocess leading and trailing whitespaces using filters,replacecan be used on stringswithinto remove whitespaces of specific patterns.

For example, if you have multiple consecutive spaces in your text and you want to replace them with a single space, you can do it like this:

{# 假设 item.Description 的值为 "这是一个   有    多余   空格的文本。" #}
{{ item.Description | replace:"   , " | replace:"  , " }}
{# 输出结果:这是一个 有 多余 空格的文本。 (通过两次替换,先将三个空格变为一个,再将两个空格变为一个,从而标准化了所有多余空格) #}

Here we use the chaining method twicereplaceFilter, first replace three consecutive spaces with a single space, then replace two consecutive spaces with a single space.This chained operation is very effective for handling irregular repeated characters.

If you want to remove all spaces in the text completely, you can also do it by taking spaces as旧字符and set新字符to be empty:

{# 假设 item.ShortText 的值为 "这是一段没有空格的文字" #}
{{ item.ShortText | replace:" ," }}
{# 输出结果:这是一段没有空格的文字 #}

Note:In the above example,replace:" ,"the intention is to remove spaces. But according to thefilter-replace.mdinstructions,oldParameter is empty, it will match at the beginning of the string and after each UTF-8 sequence. That is to say,{{"欢迎使用安企CMS"|replace:",-"}}The result is-欢-迎-使-用-安-企-C-M-S-.旧字符is a space, it will exactly replace all spaces.旧字符is an empty string.""Then, it will insert a space between each character.新字符Therefore, to remove all spaces, the correct syntax is.replace:" ,"(Note, there is a space before the comma).

3. Handle unnecessary punctuation or special symbols.

Sometimes, data imported from outside may contain punctuation or special characters that do not conform to the website style. Usingreplacefilters can help us standardize this content.

For example, if you want to remove all exclamation marks from the title of the article!and the question mark?:

{# 假设 archive.Title 的值为 "AnQiCMS 是什么?如何使用!" #}
{{ archive.Title | replace:"!," | replace:"?," }}
{# 输出结果:AnQiCMS 是什么如何使用 #}

Here we also use chaining operations, first removing Chinese exclamation marks, then removing Chinese question marks. The operation for English characters is the same.

Process more complexly with other filters

replaceThe filter can be combined with other AnQiCMS template filters to complete more complex text processing tasks. For example, you may want to clean up HTML tags from the text first and then remove certain keywords:

{# 假设 page.Content 包含 HTML 内容,并且您想移除其中的“公司”一词 #}
{{ page.Content | striptags | replace:"公司," | safe }}
{# 这里的 safe 过滤器是为了确保最终输出的文本如果还包含其他HTML字符不会被再次转义 #}

In this example,striptagsThe filter will removepage.Contentall HTML tags first, thenreplaceremove the word "company" from the cleaned text.

Useful hints while using

When usingreplaceThere are several key points to note when using the filter:

  • Case sensitive: replaceThe filter is case-sensitive by default. For example,replace:"cms,CMS"It will not replace 'CMS' with 'CMS'.
  • Literal replacement:As mentioned earlier,replacePerforming literal replacement; it does not accept regular expressions. This means you cannot use.or*special characters to match patterns; you must provide the exact string to search for.
  • Empty string used as “old character”:When you are going to旧字符Parameter set to an empty string""whenreplaceThe filter will exhibit special behavior, inserting between each UTF-8 character sequence in the original string新字符For example,{{"你好"|replace:",-"}}It will output-你-好-。Therefore, unless you explicitly need this 'insert' behavior, please avoid doing so.旧字符Set it to empty.
  • Chain operations:When you need to replace multiple different characters or phrases, you can use the pipe symbol|to chain calls.replaceFilter. Please note that the order of operations may affect the final result.

By processingreplaceFilter