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 and managing categories, more often than not, the details of content presentation, such as the uniformity of brand names, the formatting of specific keywords, even the fine-tuning of some dynamic texts, all require our precise control.Aqiy CMS provides a powerful template engine and rich filters, among whichreplaceThe filter is the tool that implements these fine adjustments.
Imagine that you are using Anqi CMS to build and maintain a website, you may need to display article titles, product descriptions, or comments submitted by users, etc.This content is usually directly read from the database and displayed on the front-end page.But sometimes, the original data retrieved from the database does not completely meet our expected display effect.For example, an old brand name needs to be updated, or a specific symbol in a text needs to be unified with another form.This may be too cumbersome and inflexible to directly modify the database data, andreplaceThe filter can provide a lightweight and efficient solution when the content is presented to the user.
IntroductionreplaceFilter: String Wizard
replaceThe filter is a very practical tool in Anqi CMS template engine, which allows us to replace a specific word with another word at the template level. Its basic usage is intuitive and easy to master, just as follows: {{ 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}}However"旧词,新词"The replacement rule, the word before the comma is the 'old word', and the word after the comma is the 'new word'.
Give an example, if the title of your article is "Welcome to AnQi CMS" but you want to replace "AnQi" with its English abbreviation "AnQi" on the front-end, you can use it like this:
{{ "欢迎使用安企CMS"|replace:"安企,AnQi" }}
After running, the result displayed on the page will be: 'Welcome to AnQiCMS'. It's like casting a small spell on a string, transforming it instantly.
Deep application: dealing with refined replacement in various scenarios
replaceThe power of the filter goes beyond this, it plays a key role in many practical scenarios:
Unified brand name and sensitive word filtering
In content operation, maintaining the consistency of the brand name is crucial. If your content may contain old and new brand names, typos, or if it is necessary to temporarily avoid displaying certain words,replaceThe filter can be put to good use. For example, if your article may contain "old brand name" and you want all the 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 or content obtained from an API or user input may not be well-formatted. For example, the phone number you get from a third-party service is123-456-7890but you hope to display it on the website without hyphens1234567890convenient for users to copy and paste:
<span>联系电话:{{ contact.Phone|replace:"-, " }}</span>
Here, we replace the hyphen with an empty string to standardize the display of the number.
The art of cleverly handling null values: the art of insertion and removal.
replaceThe filter displays some special behavior when processing an empty “old word” or “new word”, which provides us with more refined operational space:
When the “old word” is empty:If you set
oldto 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:If
newSet to an empty string, thenreplaceThe filter will simply remove the 'old word' from the string.This is a concise way to remove specific content. For example, you want to completely remove the words '安企' from a text:{{ "欢迎使用安企CMS"|replace:"安企," }}The result will be: "Welcome to CMS".
Used with other filters: to achieve more complex transformations
The AnQi CMS template filter supports chaining, which means you can connect multiple filters like a pipeline to process data in multiple steps.replaceThe filter combined with other string processing filters can achieve more complex fine-grained 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 availableupper(Convert to uppercase),trim(Remove leading and trailing spaces or specific characters),length(Get the length of a string) etc., they are related toreplaceCooperation can make your template output more flexible and beautiful.
**Practice and Precautions
AlthoughreplaceThe filter 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 AnqiCMS backend provides functions such as 'Full Station Content Replacement' and 'Document Keyword Replacement'.These features are typically used to perform batch, persistent modifications to *original data stored in the database*.For example, your old brand name has been completely abandoned, all articles need to be updated, and using the backend feature is more efficient, and the modification is permanent.
replaceTemplate filters:Used mainly for temporary and non-persistent adjustments of content during *front-end display*.It does not modify the original data in the database, it only affects the display on the current page.Suitable for scenarios involving dynamic changes, user personalized display, or quickly experimenting with different display effects.
- Performance consideration: The filter will execute every time the page is rendered. If you use a lot of complex
replaceOperate, and the content volume is huge, which may slightly affect the page loading speed. For fixed and large replacement requirements, batch processing on the backend is usually a better choice. - Maintainability:Overly complex filter chains may reduce the readability of the template. While pursuing refinement,