How to replace the old keyword with a new keyword in the AnQiCMS template?

Calendar 👁️ 62

When operating website content on AnQiCMS, we often encounter situations where we need to adjust the website text content.Sometimes it's the change of brand words, sometimes it's the correction of misspellings, or perhaps it's the temporary adjustment of copy to cater to specific marketing activities.When these modifications need to be reflected at specific locations in the website front-end template rather than the global database, AnQiCMS provides a flexible and efficient solution.

Understand the need to replace keywords within the template

The AnQiCMS backend usually provides the 'Site-wide Content Replacement' feature, which is powerful and convenient, allowing for batch modification of document content or links stored in the database.But this global replacement will directly change the original data of the website and affect all pages that call this content.

However, in certain specific scenarios, we may need to perform local, dynamic keyword replacement for a specific string in a template or for text output by a tag. For example:

  • Local tuning:A universal template snippet needs to display different keywords for specific pages, but does not want to modify the original data.
  • Formatted output:The content output by the label needs to remove certain specific symbols or phrases to meet the style of the front-end display.
  • Temporary modification:When conducting A/B tests or short-term activities, it is necessary to quickly adjust a word in the template, which can be easily restored after the event ends.

In these cases, directly using the built-in filter in the AnQiCMS template file to replace keywords becomes an ideal choice that does not affect the original data and can quickly respond to needs.

AnQiCMS solution:replaceFilter

AnQiCMS's template engine supports a rich set of filters (Filter), among which there is a special tool for string replacement—replaceA filter. Its core function is to replace the specified old keywords in a text string with new keywords and return the new string after replacement.

replaceThe basic usage of the filter is as follows:

{{ obj|replace:"旧关键词,新关键词" }}

HereobjRepresent the text string or variable you want to replace.replaceThe filter accepts a string parameter, which consists of two parts, separated by a comma in English.,Separate: The first part is what needs to be replaced旧关键词, The second part is used for replacement新关键词.

It is worth noting that if旧关键词is empty, it will match at the beginning of the string and after each UTF-8 sequence; if新关键词Empty, then it is equivalent to removing旧关键词.

Actual operation example

Let us understand through several specific examplesreplaceThe use of filters in AnQiCMS templates.

1. Basic text replacement

Suppose there is a piece of hard-coded text in the template, and one word needs to be replaced:

<p>欢迎使用安企CMS,您的内容管理专家。</p>

Now we want to replace “AnQi” with “AnQi”, and we can do it like this:

<p>{{ "欢迎使用安企CMS,您的内容管理专家。"|replace:"安企,AnQi" }}</p>

The result will be:

欢迎使用AnQiCMS,您的内容管理专家。

2. Replace keywords in dynamic content

The most common scenario is to replace keywords in the dynamic content output by AnQiCMS tags. For example, we have an article titlearchive.TitleIt is currently set to 'AnQiCMS template making guide', but we hope to replace 'AnQiCMS' with 'AnQiCMS' when displayed:

<h1>{{ archive.Title|replace:"AnQiCMS,安企CMS" }}</h1>

Ifarchive.TitleIs "AnQiCMS template creation guide", then the page will display:

<h1>安企CMS模板制作指南</h1>

3. Delete specific keywords

When you need to remove a specific keyword from a text, you can新关键词Leave blank. For example, from the article descriptionarchive.DescriptionRemove the word "exclusive":

<p>这篇文章提供了独家的AnQiCMS模板开发技巧。</p>

UsereplaceFilter deletion:

<p>{{ archive.Description|replace:"独家," }}</p>

Output Result:

<p>这篇文章提供了的AnQiCMS模板开发技巧。</p>

4. Continuously replace multiple keywords

If you need to replace the same string multiple times, you can chain the calls.replaceFilter:

<p>{{ archive.Content|replace:"公司,本公司"|replace:"版权所有,保留所有权利" }}</p>

This code will first replace "company" with "our company", and then replace "All rights reserved" with "Reserved all rights".Please note that the order of replacement may affect the final result because the result of the previous replacement will be used as the input for the next replacement.

Usage scenarios and precautions

Application scenarios:

  • Text refinement:Quickly correct minor text errors displayed on the page.
  • Brand/Term Standardization:Use the new brand name or term in the old template.
  • Content Purification:Remove unnecessary or repetitive words from the specific template output.
  • SEO Optimization Aid:For a specific page, dynamically adjust the keyword density (use with caution).

Note:

  • Performance consideration: AlthoughreplaceThe filter is highly efficient, but performing too many or too complex replacement operations on a single page may slightly affect the page rendering speed. It is recommended to use it selectively rather than abusively.
  • Maintainability:Too much replacement logic within templates can reduce the readability and maintainability of templates.If keyword replacement is a long-term and widespread need, the priority should be to perform global replacement in the content management area of the AnQiCMS backend, or modify the original data source.
  • Exact match: replaceThe filter is an exact string match. It does not use regular expressions and does not understand the context of words.Therefore, for fuzzy matching or context-aware replacement, it may be necessary to combine other AnQiCMS features or carry out secondary development.

In summary, AnQiCMS providesreplaceThe filter is a powerful and convenient tool for string operations at the template level, allowing content operation personnel to control content display more flexibly and achieve precise adjustments without touching the database.Mastering this skill will greatly enhance the efficiency of content updating and maintenance.


Frequently Asked Questions (FAQ)

Q1:replaceWhat is the difference between the filter and the 'Full Site Content Replacement' feature of AnQiCMS backend? A1: replaceThe filter is a feature provided by the AnQiCMS template engine, used to perform local replacement on the output text string of a certain tag during the rendering of the front-end page.It does not change the original content data in the database. The "full-site content replacement" feature of the backend directly modifies the content stored in the database, affecting all pages that call this content, and supports more complex replacement rules such as regular expressions, which is designed for large-scale content adjustments.The choice of method depends on whether your need is for temporary, local display adjustments, or persistent, global content data modifications.

Q2: Can I usereplaceCan regular expressions be used for more complex replacements in the filter? A2:Do not. AnQi

Related articles

How to remove the specified characters or extra spaces from the AnQiCMS string?

In website content operations, we often encounter situations where we need to process strings, such as cleaning up user input data, standardizing display content, or removing unnecessary characters and extra spaces from text.These seemingly minor operations can significantly improve the neatness and professionalism of website content.As an AnQiCMS user, you will find that the system provides powerful and flexible template filter functions that can easily meet these string processing needs.

2025-11-08

How to convert all English characters to uppercase or lowercase in the AnQiCMS template?

In the display and management of website content, maintaining uniformity in text formatting is a key factor in improving user experience and maintaining brand image.Especially the capitalization of English characters, sometimes it is necessary to unify it according to design or business needs.AnQiCMS as an efficient and flexible content management system fully considers these needs, and provides us with a quick and easy method for English character case conversion through its powerful template function.

2025-11-08

How to safely extract from an article summary containing HTML tags without damaging the HTML structure?

In website operation, the abstract of the article plays a vital role.It is not only the first window to attract visitors to click, but also an important basis for search engines to understand page content, index, and rank.A good abstract can quickly convey the core information of the article, enhance user experience, and help with SEO performance.However, when the content of the article itself contains rich HTML tags (such as images, links, bold, paragraphs, etc.), how to safely extract a summary from these contents while avoiding destroying the HTML structure has become a common challenge.

2025-11-08

How to truncate the first N characters of an AnQiCMS article title and add an ellipsis at the end?

In website operation, we often encounter such needs: in order to make the page layout beautiful, present information succinctly, or improve search engine friendliness, it is necessary to truncate the article title and add an ellipsis at the end when the title is too long.AnQiCMS (AnQiCMS) flexible template system and rich built-in filters make this operation very simple and efficient.

2025-11-08

How to make AnQiCMS automatically identify URLs and email addresses in text and convert them into clickable hyperlinks?

In daily website operations, we often need to mention URLs or email addresses in the article content.If this information is just plain text, users cannot directly click to jump, and they need to manually copy and paste, which undoubtedly increases the complexity of the operation, and may also cause users to lose interest.Fortunately, AnQiCMS provides a very simple and efficient method that allows the website to automatically identify URLs and email addresses in text and convert them into clickable hyperlinks, greatly enhancing the user experience and interactivity of the website.Why automatic recognition and conversion is so important?Imagine

2025-11-08

How to automatically truncate a long URL and display an ellipsis in the middle when displaying it in AnQiCMS?

In website content operation, we often encounter the problem that long URLs cause poor page layout and affect user experience.Especially on list pages, navigation, or other display areas, if the original URL is displayed without processing, it may take up too much space and even burst the layout.In AnQiCMS, fortunately, we can elegantly solve this problem through flexible template filters, to achieve automatic truncation of long URLs and display ellipsis at the end or at a specific position.

2025-11-08

How to automatically convert line breaks in the multi-line text entered by the user to the HTML `<br>` tag?

When using Anqi CMS to publish content, we often encounter such situations: when entering text in the content editing box, if it contains multiline information, but when the front-end page displays it, these line breaks disappear, causing the text to be squeezed together and resulting in a poor reading experience.This is usually because HTML defaults to treating newline characters (`\n`) as regular spaces.However, Anqicms provides a very convenient template filter that can help us easily solve this problem, allowing multi-line text to maintain its original formatting on the web page.### Core Function

2025-11-08

How to implement string capitalization or capitalize each word in AnQiCMS templates?

In website content operation, the way text is displayed often directly affects the reading experience and the professionalism of the website.AnQi CMS as an efficient content management system, its powerful template engine provides us with flexible content display capabilities.This includes various string formatting processes, such as what we will discuss today, how to capitalize the first letter of a string, or the first letter of each word.

2025-11-08