When operating content management systems like AnQi CMS, we often encounter scenarios where we need to replace specific text on web pages.replaceThe filter is a very practical tool in the template, but there are indeed some places where users are easy to confuse about its operation mode compared to the template variables themselves and the "Document Keyword Replacement" function provided by the system backend. Today, let's delve into it in AnQiCMS.replaceThe priority of the filter replacement operation and whether there will be a conflict with the template variables.

AnQiCMSreplaceThe basic principle of the filter.

Firstly, let's understand it.replaceThe role of the filter in the template. In the AnQiCMS Django-like template engine syntax,replaceThe filter is used to perform text replacement on the value of a string variable, replacing the 'old word' with the 'new word'. Its basic usage is very intuitive, usually like this:

{{ obj|replace:"old,new" }}

For example, if you have a variable{{ archivetitle }}The value is 'Welcome to anqi CMS', if you want to replace 'anqi' with 'anqi' when displaying on the page, you can write it like this:

{{ archivetitle|replace:"安企,anqi" }}

Here, the result displayed on the page will be “Welcome to anqiCMS”.

It is worth noting that,replaceThe filter only acts on the variable it handles.The current value. That is to say, the template engine will first parseobjthis variable, obtain its specific content (such as "Welcome to Anqi CMS"), and then use this content as input,replaceFilter performs replacement. The result after replacement is used fordisplayon the page, whileobjThe original value of the variable itself will not be permanently changed in the template.This is like copying a piece of text from a Word document, then editing the copied text, while the original document's content remains unchanged.

Therefore, from this perspective,replaceThe filter and template variables will not produce any so-called "conflict". Filters areacting onThe tools for variable values, they are in a cooperative relationship, not a competitive one. Variables provide data, and filters beautify or transform data for display.

Distinguishing and interacting with system-level content replacement

AnQiCMS not only provides at the template levelreplaceFilter, also provides more powerful 'Document Keyword Replacement' functionality in the background management feature.Under the "Content Management" module and "Document Management", we can find the batch replacement of keywords feature.This feature allows you to replace keywords or links in the entire site or specific documents in one go, even supporting regular expression replacement.

What are the differences between these two replacement methods? How will they interact?

  1. The execution time and scope are different:

    • replaceFilter:This is aTemplate rendering timeThe operation to be performed.It only performs temporary processing on the data passed to the template when generating HTML pages, without modifying the original content stored in the database.Its range of action is limited to the variables applied the filter in the current template.
    • System-level "Document Keyword Replacement":This is aWhen the background management is being performedThe operation to be executed. It will directly modifyThe original document content stored in the databaseThis means that once you have performed the replacement in the background, the content has already changed before being loaded into any template.
  2. Priority and interaction: Understood the difference in the execution timing of the two, their priority is evident:The priority of system-level content replacement is higher than that of templatesreplaceFilter.

    When a document content is replaced by a system-level feature, if this document content is then passed to the front end through a template variable, and a filter is applied to this variable,replaceFilter, thenreplacethe filter will act onThe content has been replaced by the backgroundNew content.

    For example:

    • Suppose the original content of a document is:“Our company sellsAppleMobile phone.”
    • You are using the 'Document Keyword Replacement' feature in the background, replacing all occurrences of 'apple' with 'orange'. At this point, the content in the database has become: 'Our company sells'}]orangeMobile phone.”
    • Now, you assign the content of this document to a variable in the template{{ article.content }}and applyreplacea filter to try to replace 'apple' with 'banana':
      
      {{ article.content|replace:"苹果,香蕉" }}
      
    • 最终显示在页面上的结果将是:“我们公司销售orange手机。” 为什么?因为在article.contentThe variable was parsed by the template engine before 'apple' in the database had turned into 'orange'.replaceThe filter cannot find the word "apple" when executing, so it will not replace anything.

    On the contrary, if the background replaces 'apple' with 'orange', and the template filter wants to replace 'orange' with 'banana':

    {{ article.content|replace:"橘子,香蕉" }}
    

    Then the page will display: 'Our company sellsBananaMobile.” Because the backend has already changed “Apple” to “Orange”, the filter then replaces “Orange”, and everything happens in order.

    This is not the so-called 'conflict', but the difference in 'execution order' and 'scope'.

Practical suggestion: how to make reasonable use of two replacement methods

UnderstoodreplaceAfter the characteristics of filters and system-level content replacement, we can better plan content operation strategies:

  • UsereplaceFilter (template level):

    • Applicable scenarios:The display content of a specific variable needs to be temporarily and dynamically formatted or corrected without affecting the original content.For example, replace sensitive words in the article summary with asterisks, or adjust the specific text of a field according to different frontend display requirements.This is usually a 'what you see is what you get' display requirement.
    • Advantages:Flexible, does not modify original data, easy to test and rollback, does not affect SEO (because search engines crawl the original content).
    • Disadvantages:Cannot process in bulk, there is a performance overhead for each rendering (although usually small).
  • Use 'Document Keywords Replacement' in the background (at the system level):

    • Applicable scenarios:需要对大量内容进行全局性、永久性的修改,例如:公司名称变更、产品品牌升级、大规模URL调整导致旧链接需要更新为新链接、内容错别字批量修正、SEO关键词布局调整(例如将某些关键词替换为锚文本)。This is usually the requirement for 'Modify Content Source'.
    • Advantages:Batch, efficient, directly modify the database, directly affects SEO.
    • Disadvantages:Cannot be undone (unless there is a backup), improper operation