In the daily operation of AnQi CMS, we often encounter situations where we need to flexibly adjust the website content.This is not just about publishing new articles or managing categories; more often, the details of content presentation, such as the uniformity of brand names, the formatting of specific keywords, and even the fine-tuning of some dynamic text, all require our refined control.replaceThe filter is the tool that enables these refined adjustments.

replaceThe filter provides a lightweight and efficient solution when content is presented to the user.

IntroductionreplaceFilter: The Alchemist of Strings

replaceThe filter is a very practical tool in the Anqi CMS template engine, allowing us to replace a specific word in a string with another word at the template level. Its basic usage is intuitive and easy to master, and can be written like this:{{ obj|replace:"旧词,新词" }}.

Here,objRepresents the string variable you want to operate on, such as the title of an article{{archive.Title}}or a description{{item.Description}}."旧词,新词"The content is the replacement rule, the word before the comma is the 'old word' to be replaced, and the word after the comma is the 'new word' after replacement.

For example, if your article title is “Welcome to AnQi CMS”, but you want to display the English abbreviation “AnQi” on the front end instead, you can use it like this:

{{ "欢迎使用安企CMS"|replace:"安企,AnQi" }}

After running, the result displayed on the page will be: 'Welcome to AnQiCMS'. This is like casting a little magic on the string, transforming it instantly.

In-depth Application: Fine-grained Replacement for Various Scenarios

replaceThe power of the filter goes far beyond this, and it plays a crucial role in various practical scenarios:

Unified Brand Name and Sensitive Word Filtering

It is crucial to maintain consistency in brand names in content operation. If your content may contain old and new brand names, typos, or need to temporarily hide certain words, replaceThe filter can be put to use. For example, if there may be 'old brand name' in your article, and you wish all displays to be unified as 'new brand name', you can handle it like this in the template:

<p>{{ item.Content|replace:"老品牌名称,新品牌名称" }}</p>

Format dynamic content

Data obtained from an API or user input may not be in an ideal format. For example, the phone number you obtain from a third-party service may be123-456-7890[en] But you hope to display it on the website without hyphens:1234567890[en] For easy copying and pasting:

<span>联系电话:{{ contact.Phone|replace:"-, " }}</span>

Here, we replace the hyphen with an empty string to achieve standardized display of the number.

Art of handling null values: the art of insertion and removal.

replaceThe filter exhibits some special behaviors when the 'old word' or 'new word' is empty, which provides us with more space for refined operations:

  • When the 'old word' is empty:If you setoldto an empty string,replacethe filter will magically insert at the beginning of the original string and after each UTF-8 character sequence.newThis is very useful when you need to add separators for each character. For example, you want to insert a “-” between each Chinese character:

    {{ "欢迎使用安企CMS"|replace:",-" }}
    

    This will output: "-WELCOME-TO-USE-ANQI-CMS-".Please note that this usage is very powerful, but it also requires caution to avoid unexpected full-site character insertion.

  • When the “new word” is empty:IfnewIf set to an empty string, it will be:replaceThe filter will simply remove the "old words" from the string.This is a concise way to remove specific content.

    {{ "欢迎使用安企CMS"|replace:"安企," }}
    

    The result will be: "Welcome to CMS".

Combined with other filters: to achieve more complex transformations

The template filters of AnQi CMS support chained calls, which means you can connect multiple filters together like a pipeline and process the data in multiple steps.replaceThe filter combined with other string processing filters can achieve more complex and refined control.

For example, you may need to convert the text to lowercase first, then replace a keyword, and finally truncate to a specified length:

{{ item.Title|lower|replace:"cms,content management system"|truncatechars:50 }}

Here,lowerThe filter first converts the title to lowercase,replaceReplaced "cms" with "content management system", finallytruncatechars:50Truncated the entire string to 50 characters and added an ellipsis.

There are similar filters such asupper(converted to uppercase),trim(removing leading and trailing spaces or specific characters),length(getting the length of a string) and others, they are withreplaceCooperation can make your template output more flexible and beautiful.

**Practical Tips and Considerations

AlthoughreplaceThe filter feature is powerful, but in actual use, we still need to follow some **practices to ensure the performance and maintainability of the website:

  • When to use backend replacement, when to use template filters:
    • Backend 'Full Site Content Replacement' feature:The backend of AnQi CMS provides features such as "Full Site Content Replacement" and "Document Keyword Replacement".These features are usually used for batch and persistent modification of *original data stored in the database*.For example, if your old brand name has been completely abandoned and all articles need to be updated, using the backend feature is more efficient, and the modification is permanent.
    • replaceTemplate filters:主要用于*front-end display* for temporary and non-persistent adjustments to content.It does not modify the original data in the database, it only affects the display on the current page.Suitable for scenarios with dynamic changes, user personalized display, or rapid experiments with different display effects.
  • Performance considerations:The filter will execute every time the page renders. If your page uses a lot of complexreplaceOperation, and the content volume is large, which may have a slight impact on page loading speed. For fixed and large-scale replacement needs, backend batch processing is usually a better choice.
  • Maintainability:Overly complex filter chains may reduce the readability of the template. While pursuing granularity,